我想在Treeview中显示以下集合
private ObservableCollection<SectionHeader> _sections;
public ObservableCollection<SectionHeader> Sections
{
get { return _sections ?? (_sections = new ObservableCollection<SectionHeader>()); }
set { _sections = value; NotifyOfPropertyChange(() => Sections); }
}
以下是SectionHeader和嵌套类型的外观
public class SectionHeader
{
public string ID { get; set; }
public string Name { get; set; }
private ObservableCollection<SectionItem> _items;
public ObservableCollection<SectionItem> Items { get { return _items ?? (_items = new ObservableCollection<SectionItem>()); } }
}
public class SectionItem
{
public string Title { get; set; }
public int ID { get; set; }
private ObservableCollection<ProductCalculatorTemplate> _products;
public ObservableCollection<ProductCalculatorTemplate> Products { get { return _products ?? (_products = new ObservableCollection<ProductCalculatorTemplate>()); } }
private ObservableCollection<ProductCalculatorTemplate> _productsOptionTwo;
public ObservableCollection<ProductCalculatorTemplate> ProductsOptionTwo { get { return _productsOptionTwo ?? (_productsOptionTwo = new ObservableCollection<ProductCalculatorTemplate>()); } }
}
public class ProductCalculatorTemplate
{
public string Product { get; set; }
public double NoOfCoats { get; set; }
public double PackSize { get; set; }
}
以下是我的TreeView的XAML代码如何
<TreeView ItemsSource="{Binding Sections}" Background="GhostWhite">
<TreeView.Resources>
<HierarchicalDataTemplate DataType= "{x:Type ViewModels:SectionHeader}" ItemsSource = "{Binding Path=Items}">
<StackPanel Orientation="Horizontal" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType = "{x:Type ViewModels:SectionItem}" ItemsSource = "{Binding Path=Products}">
<StackPanel Orientation="Horizontal" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
<TextBlock Text="{Binding Title}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType = "{x:Type ViewModels:ProductCalculatorTemplate}" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Product}"/>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
这是上述代码的输出
但是我想以一种方式显示信息,在Fenomastic下面会出现一个名为“Option 1”的子节点,它应该列出所有产品,而Fenomastic的第二个子应该是“Option 2”,它应该列出SectionItem的所有ProductsOptionTwo成员。
答案 0 :(得分:1)
仅使用WPF中的基本树视图,最简单的选择是引入一个选项集合,其中包含选项1和选项2作为项目,即:
public class SectionItem
{
public string Title { get; set; }
public int ID { get; set; }
private ObservableCollection<Option> _options;
public ObservableCollection<Option> Options
{
get { return _options ?? (_options = new ObservableCollection<Option>()); }
}
}
public class Option
{
public string Name { get; set; }
public ObservableCollection<ProductCalculatorTemplate> Products
{
get { return _products ?? (_products = new ObservableCollection<ProductCalculatorTemplate>()); }
}
}
为HierarchicalDataTemplate
类型添加另一个Option
由于TreeView
不适合列出不可变数量的属性,例如ProductsOption
和ProductsOptionTwo
属性。