DataTemplateSelector in WPF

本文关键字:WPF in DataTemplateSelector | 更新日期: 2023-09-27 18:07:32

我想在两个具有不同绑定和控件的视图之间切换。我可以这样做使用DataTemplateSelector?

<TabControl
        ItemsSource="{Binding Items}" SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay}">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <DockPanel>
                    <TextBlock Text="{Binding TabName}"><TextBlock.Background><SolidColorBrush /></TextBlock.Background></TextBlock>
                    <Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0"  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}" BorderBrush="#00000000">
                        <Image Source="/WPF_AccApp;component/Images/11.gif" Height="11" Width="11"></Image>
                    </Button>
                    <DockPanel.Background>
                        <SolidColorBrush />
                    </DockPanel.Background>
                </DockPanel>
            </DataTemplate>
        </TabControl.ItemTemplate>
                <DataTemplate>
                        <y:TabView /> //Here I want to have two diferent views
                    </DataTemplate>
            </TabControl>

DataTemplateSelector in WPF

实际上,这取决于切换逻辑和视图模型的设计方式。可能有不止一个解。例如,这是一个没有DataTemplateSelector的示例,它是基于样式触发器的。

视图模型:

public class ItemVm
{
    public string Name { get; set; }
    public bool IsSelected { get; set; }
    public int X { get; set; }
    public int Y { get; set; }
}
XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!-- This one chooses the view -->
    <CheckBox x:Name="ViewSelector" Content="View shapes"/>
    <TabControl Grid.Row="1" ItemsSource="{Binding}">
        <TabControl.Resources>
            <DataTemplate x:Key="TextualTemplateKey">
                <StackPanel>
                    <TextBlock Text="{Binding X}"/>
                    <TextBlock Text="{Binding Y}"/>
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="ShapesTemplateKey">
                <Rectangle Fill="Green" Width="{Binding X}" Height="{Binding Y}"/>
            </DataTemplate>
        </TabControl.Resources>
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                <Setter Property="ContentTemplate" Value="{StaticResource TextualTemplateKey}"/>
                <Style.Triggers>
                    <!-- When "View shapes" is checked, we're changing data template to a new one -->
                    <DataTrigger Binding="{Binding IsChecked, ElementName=ViewSelector}" Value="True">
                        <Setter Property="ContentTemplate" Value="{StaticResource ShapesTemplateKey}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TabControl.ItemContainerStyle>
    </TabControl>
</Grid>

DataTemplateSelector允许您实现更复杂的逻辑,但也有它自己的缺点:如果您想从视图中获得一些东西,您必须遍历元素树。