如何在 TreeView WPF 中绑定相同级别的相同类型的集合
How to bind collections with same type on same level in TreeView WPF
我想在树视图中显示以下集合
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; }
}
这是我的 XAML TreeView 代码的样子
<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" 的子节点,它应该列出所有 Prdoucts,而 Fenomastic 的第二个子节点应该是 "Option 2",它应该列出所有 ProductsOptionTwo 成员SectionItem.
仅使用 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>()); }
}
}
并为 Option
类型添加另一个 HierarchicalDataTemplate
因为 TreeView
不适合像您的 ProductsOption
和 ProductsOptionTwo
属性那样列出固定数量的属性。
我想在树视图中显示以下集合
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; }
}
这是我的 XAML TreeView 代码的样子
<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>
这是上面代码的输出
仅使用 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>()); }
}
}
并为 Option
类型添加另一个 HierarchicalDataTemplate
因为 TreeView
不适合像您的 ProductsOption
和 ProductsOptionTwo
属性那样列出固定数量的属性。