首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF中的DataTemplateSelector

WPF中的DataTemplateSelector
EN

Stack Overflow用户
提问于 2015-08-13 13:32:10
回答 1查看 7.7K关注 0票数 0

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

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2015-08-13 14:19:59

实际上,这取决于切换逻辑和视图模型是如何设计的。可以有多个解决方案。例如,这里的示例根本没有DataTemplateSelector,它是基于样式触发器的。

查看模型:

代码语言:javascript
复制
public class ItemVm
{
    public string Name { get; set; }
    public bool IsSelected { get; set; }
    public int X { get; set; }
    public int Y { get; set; }
}

XAML:

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31980158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档