繁体   English   中英

WPF中的DataTemplateSelector

[英]DataTemplateSelector in WPF

我想在具有不同绑定和控件的两个视图之间切换。 我可以使用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 ,它基于样式触发器。

查看模型:

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允许您实现更复杂的逻辑,但是也有其缺点:如果要从视图中获取某些内容,则必须遍历元素树。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM