我正在使用树视图,我希望有一个多级树视图,可以对树的每个级别(所有子项)进行分组。
它似乎适用于第一级别的项目,但当项目下面有子项时,分组样式似乎并未应用。
My Xaml Group Style,HierarchicalDataTemplate,TreeView
<DataTemplate x:Key="basicGroupStyle">
<!--Hide the whole grouping header if the group name is empty string-->
<DockPanel LastChildFill="True" >
<Border Background="RoyalBlue"
Margin="0" MinHeight="3" Width="5"
VerticalAlignment="Center" DockPanel.Dock="Left"/>
<TextBlock Text="{Binding Name}"
Foreground="Black"
FontSize="8pt" FontWeight="Bold"
VerticalAlignment="Center"
Margin="5 0 5 0"
DockPanel.Dock="Left"/>
<Border Background="RoyalBlue"
Margin="0" MinHeight="3"
VerticalAlignment="Center"/>
</DockPanel>
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type explorer:TreeItem}"
ItemsSource="{Binding TreeViewItemsView}">
<!--Hide the whole grouping header if the group name is empty string-->
<DockPanel LastChildFill="True" >
<TextBlock Text="{Binding Name}"
Foreground="Black"
FontSize="8pt" FontWeight="Bold"
VerticalAlignment="Center"
Margin="5 0 5 0"
DockPanel.Dock="Left"/>
</DockPanel>
</HierarchicalDataTemplate>
<TreeView ItemsSource="{Binding TreeViewItemsView}">
<TreeView.GroupStyle>
<GroupStyle HeaderTemplate="{StaticResource basicGroupStyle}" />
</TreeView.GroupStyle>
</TreeView>
转换器/组说明
public class AssetTypeExplorerValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var v = value as int?;
if (v == null)
return value;
return Convert(v);
}
public static string Convert(int? assetData)
{
if (assetData.HasValue)
{
if (assetData == 1)
return "First Group";
else if (assetData == 2)
return "Second Group";
else if (assetData == 3)
return "Third Group";
else
return "Damn";
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
TreeItem类
public int level { get; set; }
public string Name { get; set; }
private ICollectionView _treeViewItemsView;
private ObservableCollection<TreeItem> _treeViewItems;
public ObservableCollection<TreeItem> TreeViewItems
{
get { return _treeViewItems ?? (TreeViewItems = new ObservableCollection<TreeItem>()); }
set { _treeViewItems = value; }
}
public ICollectionView TreeViewItemsView
{
get
{
if (_treeViewItemsView == null)
{
_treeViewItemsView = CollectionViewSource.GetDefaultView(TreeViewItems) as ListCollectionView;
}
return _treeViewItemsView;
}
}
private void Initalize()
{
(this.TreeViewItemsView as ListCollectionView).GroupDescriptions.Clear();
(this.TreeViewItemsView as ListCollectionView).GroupDescriptions.Add(new PropertyGroupDescription("level", new AssetTypeExplorerValueConverter()));
}
TreeItems的集合(在构造函数上调用Initalize)
private ICollectionView _treeViewItemsView;
private ObservableCollection<TreeItem> _treeViewItems;
public ICollectionView TreeViewItemsView
{
get
{
if (_treeViewItemsView == null)
{
_treeViewItemsView = CollectionViewSource.GetDefaultView(TreeViewItems) as ListCollectionView;
}
return _treeViewItemsView;
}
}
public ObservableCollection<TreeItem> TreeViewItems
{
get { return _treeViewItems ?? (TreeViewItems = new ObservableCollection<TreeItem>()); }
set { _treeViewItems = value; }
}
private void SetupItems()
{
var first = new TreeItem() { Name = "First Level First Item", level = 1 };
var secLevel = new TreeItem() { Name = "Second Level First Item", level = 2 };
first.TreeViewItems.Add(secLevel);
TreeViewItems.Add(first);
TreeViewItems.Add(new TreeItem() { Name = "Second Item", level = 1 });
TreeViewItems.Add(new TreeItem() { Name = "Third Item", level = 1 });
}
private void Initalize()
{
(this.TreeViewItemsView as ListCollectionView).GroupDescriptions.Clear();
(this.TreeViewItemsView as ListCollectionView).GroupDescriptions.Add(new PropertyGroupDescription("level", new AssetTypeExplorerValueConverter()));
SetupItems();
}
知道为什么会这样吗? 任何帮助将不胜感激!谢谢!
答案 0 :(得分:0)
一个建议;
将HierarchicalDatatemplate分配给TreeView。
<HierarchicalDataTemplate x:Key="TestTemplate" .... HierarchicalDataTemplate>
<TreeView ItemTemplate="{StaticResource TestTemplate}" ..... TreeView>