2

我有这个简单的 XAML 示例:

<Window x:Class="DynTemplateTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <DataTemplate x:Key="ItemTemplate">
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Rectangle Width="30" Height="30" Fill="Red"></Rectangle>                        
                    </DataTemplate>
                </ItemsControl.ItemTemplate>  
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style>
                        <Setter Property="Canvas.Left" Value="{Binding Position}"></Setter>
                    </Style>
                </ItemsControl.ItemContainerStyle>
            </ItemsControl>
        </DataTemplate>
    </Window.Resources>

    <DockPanel LastChildFill="True">
        <ContentPresenter
            Content="{Binding Path=Items}"
            ContentTemplate="{StaticResource ItemTemplate}"
            >
        </ContentPresenter>
    </DockPanel>

</Window>

它以 MVVM 样式在可观察集合中呈现我的项目。每个项目在属性中都有其水平位置。每个项目还有一个属性 IsSpecial ,它告诉它是否想以某种特殊的方式呈现。我希望普通项目(IsSpecial=false)呈现为红色方块(已经在代码中),特殊项目呈现为蓝色圆圈,里面有“特殊”文本。

我不知道如何调整 XAML 代码来为项目进行模板选择。有没有办法在不编写我自己的 ItemTemplateSelector 的情况下做到这一点?它是否仍然适用于基于绑定的画布定位。我认为解决方案是将项目模板提取到一个单独的模板中,为特殊项目再创建一个模板,然后以某种方式使用触发器......但这对我来说并不容易,因为我目前是 WPF 初学者。

另一件事是我非常不喜欢将位置传递给项目的方式。还有其他方法吗?

还有其他建议如何改进代码吗?

4

1 回答 1

5

我自己解决了:D

<Window x:Class="DynTemplateTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <DataTemplate x:Key="NormalItem">
            <Rectangle Width="30" Height="30" Fill="Red"></Rectangle>                        
        </DataTemplate>
        <DataTemplate x:Key="SpecialItem">
            <Rectangle Width="30" Height="30" Fill="Red"></Rectangle>                        
        </DataTemplate>
        <DataTemplate x:Key="ItemTemplate">
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding}" ContentTemplate="{StaticResource NormalItem}" x:Name="ItemsContentControl" />
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=IsSpecial}" Value="true">
                                <Setter TargetName="ItemsContentControl" Property="ContentTemplate" Value="{StaticResource SpecialItem}" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>  
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style>
                        <Setter Property="Canvas.Left" Value="{Binding Position}" />
                    </Style>
                </ItemsControl.ItemContainerStyle>
            </ItemsControl>
        </DataTemplate>
    </Window.Resources>

    <DockPanel LastChildFill="True">
        <ContentPresenter
            Content="{Binding Path=Items}"
            ContentTemplate="{StaticResource ItemTemplate}"
            >
        </ContentPresenter>
    </DockPanel>

</Window>

但是,关于替代方案或改进的任何想法?

于 2009-06-30T08:23:44.460 回答