我有TreeView(名为:treeViewDiagram):
< TreeView x:Name = "treeViewDiagram" Grid.Row="2"
FontFamily="/logicalTree;component/Resources/Fonts/#Oxygen"
BorderThickness="7,7,0,7" BorderBrush="#FFE9E9E9" Padding="86,0,0,0" ItemTemplateSelector="{Binding Source={StaticResource dataTemplateSelector}}" />
这里有三个dataTamplate:
<local:CostomDataTemplateSelector x:Key="dataTemplateSelector"/>
< DataTemplate x:Key = "BasicDataTemplate">
< Grid Height="Auto" MinWidth="250" HorizontalAlignment="Left" VerticalAlignment="Top" >
<...>
< /Grid >
< /DataTemplate >
<DataTemplate x:Key="ComplexDataTemplate" >
<Grid Height="Auto" MinWidth="250" HorizontalAlignment="Left" VerticalAlignment="Top">
<...>
</Grid>
</DataTemplate>
<DataTemplate x:Key="RootDataTemplate" >
<Grid Height="Auto" MinWidth="250" HorizontalAlignment="Left" VerticalAlignment="Top" >
<...>
</Grid>
</DataTemplate>
现在,我想在代码中添加treeViewItems并为每个treeViewItem设置 不同的dataTemplate根据我的代码。
(if(basic)=&gt; so:BasicDataTemplate)
(if(complex)=&gt; so:ComplexDataTemplate)
(if(root)=&gt; so:RootDataTemplate)
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
FrameworkElement element = container as FrameworkElement;
if (item is MyBasicData)
{
return element.FindResource("BasicDataTemplate") as DataTemplate;
}
else if (item is MyComplexData)
{
return element.FindResource("ComplexDataTemplate") as DataTemplate;
}
else if (item is MyRootData)
{
return element.FindResource("RootDataTemplate") as DataTemplate;
}
return null;
}
我在MainWindow中有3个ObservableCollection:
private ObservableCollection< MyBasicData > _myBasicDataCollection = new ObservableCollection< MyBasicData >();
private ObservableCollection< MyComplexData > _myComplexDataCollection = new ObservableCollection< MyComplexData >();
private ObservableCollection< MyRootData > _myRootDataCollection = new ObservableCollection< MyRootData >();
我该怎么做?
答案 0 :(得分:0)
创建 DataTemplateSelector ,它遍历每个项目,并根据条件设置其DataTemplate。
class TreeViewTemplateSelector : DataTemplateSelector
{
public DataTemplate BasicTemplate { get; set; }
public DataTemplate ComplexTemplate { get; set; }
public DataTemplate RootTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is MyBasicData) return BasicTemplate;
if (item is MyComplexData) return ComplexTemplate;
if (item is MyRootData) return RootTemplate;
return null;
}
}
XAML:
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication6">
<Window.Resources>
<DataTemplate x:Key="BasicDataTemplate">
</DataTemplate>
<DataTemplate x:Key="ComplexDataTemplate">
</DataTemplate>
<DataTemplate x:Key="RootDataTemplate">
</DataTemplate>
<local:TreeViewTemplateSelector x:Key="TreeViewDataTemplateSelector" BasicTemplate="{StaticResource BasicDataTemplate}"
ComplexTemplate="{StaticResource ComplexDataTemplate}"
RootTemplate="{StaticResource RootDataTemplate}"/>
</Window.Resources>
<TreeView ItemTemplateSelector="{StaticResource TreeViewDataTemplateSelector}"/>