将集合绑定到 wpf 中的树视图
binding collection to treeview in wpf
我是 c# wpf 的新手。我正在尝试制作一个演示应用程序,它使用 treeview 来显示分层数据。到目前为止,我已经完成了以下工作。
- ViewModelBase.cs
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
- Item.cs
public class Item : ViewModelBase
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public ObservableCollection<Item> Children { get; private set; }
public Item() : this("Item") { }
public Item(string name)
{
Children = new ObservableCollection<Item>();
}
}
- MainWindow.xaml
<Window x:Class="TreeViewDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TreeViewDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TreeView x:Name="TreeView_Items" Grid.Row="0" Grid.Column="0" ItemsSource="{ Binding }">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{ Binding Path=Children }" DataType="{ x:Type local:Item }">
<TextBlock Text="{ Binding Path=Name }" Foreground="Black" Visibility="Visible" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<TreeView x:Name="TreeView_Items1" Grid.Row="0" Grid.Column="1">
<TreeViewItem Header="Item">
<TreeViewItem Header="Item" />
<TreeViewItem Header="Item" />
</TreeViewItem>
<TreeViewItem Header="Item">
<TreeViewItem Header="Item">
<TreeViewItem Header="Item" />
</TreeViewItem>
<TreeViewItem Header="Item" />
</TreeViewItem>
<TreeViewItem Header="Item" />
</TreeView>
<Button x:Name="Button_Add" Grid.Row="1" Grid.Column="0" Content="Add" Click="Button_Add_Click" />
<Button x:Name="Button_Test" Grid.Row="1" Grid.Column="1" Content="Test" />
</Grid>
</Window>
- MainWindow.xaml.cs
public partial class MainWindow : Window
{
public ObservableCollection<Item> Items { get; } = new ObservableCollection<Item>();
public MainWindow()
{
InitializeComponent();
Items.Add(new Item("Parent"));
TreeView_Items.ItemsSource = Items;
}
private void Button_Add_Click(object sender, RoutedEventArgs e)
{
var selected = TreeView_Items.SelectedItem as Item;
if (selected == null)
Items.Add(new Item("Parent"));
else
selected.Children.Add(new Item("Item"));
}
}
问题是当我编译时 运行 它的 treeviewitems 没有出现在 treeview 中,也没有大喊任何错误。当我检查实时可视化树时,treeview 将文本块作为子项而不是 treeviewitem。
live visual tree image。
如果我单击添加按钮,它只会添加另一个文本块而不是 treeviewitem。谁能告诉我我做错了什么?
这是因为您没有在项目中设置名称 class。正在添加项目,但由于名称为空,您看不到任何内容。
public Item(string name)
{
Name = name;
Children = new ObservableCollection<Item>();
}
我是 c# wpf 的新手。我正在尝试制作一个演示应用程序,它使用 treeview 来显示分层数据。到目前为止,我已经完成了以下工作。
- ViewModelBase.cs
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
- Item.cs
public class Item : ViewModelBase
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public ObservableCollection<Item> Children { get; private set; }
public Item() : this("Item") { }
public Item(string name)
{
Children = new ObservableCollection<Item>();
}
}
- MainWindow.xaml
<Window x:Class="TreeViewDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TreeViewDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TreeView x:Name="TreeView_Items" Grid.Row="0" Grid.Column="0" ItemsSource="{ Binding }">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{ Binding Path=Children }" DataType="{ x:Type local:Item }">
<TextBlock Text="{ Binding Path=Name }" Foreground="Black" Visibility="Visible" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<TreeView x:Name="TreeView_Items1" Grid.Row="0" Grid.Column="1">
<TreeViewItem Header="Item">
<TreeViewItem Header="Item" />
<TreeViewItem Header="Item" />
</TreeViewItem>
<TreeViewItem Header="Item">
<TreeViewItem Header="Item">
<TreeViewItem Header="Item" />
</TreeViewItem>
<TreeViewItem Header="Item" />
</TreeViewItem>
<TreeViewItem Header="Item" />
</TreeView>
<Button x:Name="Button_Add" Grid.Row="1" Grid.Column="0" Content="Add" Click="Button_Add_Click" />
<Button x:Name="Button_Test" Grid.Row="1" Grid.Column="1" Content="Test" />
</Grid>
</Window>
- MainWindow.xaml.cs
public partial class MainWindow : Window
{
public ObservableCollection<Item> Items { get; } = new ObservableCollection<Item>();
public MainWindow()
{
InitializeComponent();
Items.Add(new Item("Parent"));
TreeView_Items.ItemsSource = Items;
}
private void Button_Add_Click(object sender, RoutedEventArgs e)
{
var selected = TreeView_Items.SelectedItem as Item;
if (selected == null)
Items.Add(new Item("Parent"));
else
selected.Children.Add(new Item("Item"));
}
}
问题是当我编译时 运行 它的 treeviewitems 没有出现在 treeview 中,也没有大喊任何错误。当我检查实时可视化树时,treeview 将文本块作为子项而不是 treeviewitem。 live visual tree image。 如果我单击添加按钮,它只会添加另一个文本块而不是 treeviewitem。谁能告诉我我做错了什么?
这是因为您没有在项目中设置名称 class。正在添加项目,但由于名称为空,您看不到任何内容。
public Item(string name)
{
Name = name;
Children = new ObservableCollection<Item>();
}