WPF 中的 DataTemplateSelector
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31980158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataTemplateSelector in WPF
提问by Mrg Gek
I want to switch between two views that have different bindings and controls. Can I do this using DataTemplateSelector?
我想在具有不同绑定和控件的两个视图之间切换。我可以使用 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>
回答by Dennis
Actually, it depends on switching logic and how view models are designed. There can be more than one solution. E.g., here's sample without DataTemplateSelectorat all, it is based on style trigger.
实际上,这取决于切换逻辑以及视图模型的设计方式。可能有不止一种解决方案。例如,这里的示例DataTemplateSelector根本没有,它基于样式触发器。
View model:
查看型号:
public class ItemVm
{
public string Name { get; set; }
public bool IsSelected { get; set; }
public int X { get; set; }
public int Y { get; set; }
}
XAML:
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>
DataTemplateSelectorallows you to implement more sophisticated logic, but also has its own cons: if you want to get something from the view, you have to walk through the elements tree.
DataTemplateSelector允许你实现更复杂的逻辑,但也有它自己的缺点:如果你想从视图中得到一些东西,你必须遍历元素树。

