[英]WPF TreeView-How to refresh tree after adding/removing node?
I refer to this article: 我参考这篇文章:
WPF TreeView HierarchicalDataTemplate - binding to object with multiple child collections WPF TreeView HierarchicalDataTemplate - 绑定到具有多个子集合的对象
and modify the tree structure like: 并修改树结构,如:
Root
|__Group
|_Entry
|_Source
In Entry.cs: 在Entry.cs中:
public class Entry
{
public int Key { get; set; }
public string Name { get; set; }
public ObservableCollection<Source> Sources { get; set; }
public Entry()
{
Sources = new ObservableCollection<Source>();
}
public ObservableCollection<object> Items
{
get
{
ObservableCollection<object> childNodes = new ObservableCollection<object>();
foreach (var source in this.Sources)
childNodes.Add(source);
return childNodes;
}
}
}
In Source.cs: 在Source.cs中:
public class Source
{
public int Key { get; set; }
public string Name { get; set; }
}
In XAML file: 在XAML文件中:
<UserControl.CommandBindings>
<CommandBinding Command="New" Executed="Add" />
</UserControl.CommandBindings>
<TreeView x:Name="TreeView">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="TreeViewItem.IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Root}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Path=Name}" IsEnabled="True">
</TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Path=Name}" IsEnabled="True">
</TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Entry}" ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" IsEnabled="True">
<TextBlock.ContextMenu>
<ContextMenu >
<MenuItem Header="Add" Command="New">
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:Source}" >
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</TreeView.Resources>
</TreeView>
In UserControl.cs: 在UserControl.cs中:
public ObservableCollection<Root> Roots = new ObservableCollection<Root>();
public UserControl6()
{
InitializeComponent();
//...Add new node manually
TreeView.ItemsSource = Roots;
}
private void Add(object sender, ExecutedRoutedEventArgs e)
{
Entry ee = (Entry)TreeView.SelectedItem;
Source s3 = new Source() { Key = 3, Name = "New Source" };
ee.Sources.Add(s3);
}
When I click right button on specific node "Entry" to add a new node "Source" under Entry (call "Add" method), I add a new "Source" object under Entry successfully, but I can't see this new node on treeview. 当我在特定节点“Entry”上单击右键以在Entry(调用“Add”方法)下添加新节点“Source”时,我在Entry下成功添加了一个新的“Source”对象,但是我看不到这个新节点在树视图上。 How to refresh treeview when adding/deleting node?
添加/删除节点时如何刷新树视图?
如果要通知用户界面集合中的某些内容已更改,请使用ObservableCollection而不是IList
As far as I'm concerned, changing of type for Items
to ObservableCollection<T>
will not resolve the problem. 就我而言,将
Items
的类型更改为ObservableCollection<T>
将无法解决问题。 You need to implement INotifyPropertyChanged
. 您需要实现
INotifyPropertyChanged
。 I tested both solutions for my tree view, because I faced the same problem. 我为树视图测试了两个解决方案,因为我遇到了同样的问题。 In my case changing of type from IList to ObservableCollection didn't refreshed GUI.
在我的情况下,从IList到ObservableCollection的类型更改没有刷新GUI。 However when I changed my auto property:
但是当我改变我的汽车财产时:
public List<SourceControlItemViewBaseModel> Items { get; set; }
to 至
private IEnumerable<SourceControlItemViewBaseModel> _items;
public IEnumerable<SourceControlItemViewBaseModel> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged();
}
}
Namely, I've implemented INotifyPropertyChanged
and that changed the situation. 也就是说,我已经实现了
INotifyPropertyChanged
,这改变了这种情况。 The method that builds the tree structure defines the actual type of Items
as new List<T>()
, but it works and refreshes the GUI . 构建树结构的方法将
Items
的实际类型定义为新的List<T>()
,但它可以工作并刷新GUI。 Nevertheless my tree was built in pure MVVM pattern without usage code-behind. 然而,我的树是用纯MVVM模式构建的,没有使用代码隐藏。 I use
我用
<TreeView ItemsSource="{Binding SourceControlStructureItems}" />
and in the view model I use: 在我使用的视图模型中:
currentVm.Items= await SourceControlRepository.Instance.BuildSourceControlStructureAsync(currentVm.ServerPath);
That means I didn't added/removed items, but I rebuilt Node's sub collection. 这意味着我没有添加/删除项目,但我重建了Node的子集合。
Use this class and any changes in Sources collection will update/refresh tree in UI. 使用此类,Sources集合中的任何更改都将在UI中更新/刷新树。
public class Entry
{
public int Key { get; set; }
public string Name { get; set; }
public ObservableCollection<Source> Sources { get; set; }
public Entry()
{
Sources = new ObservableCollection<Source>();
}
public CompositeCollection Items
{
get
{
return new CompositeCollection()
{
new CollectionContainer() { Collection = Sources },
// Add other type of collection in composite collection
// new CollectionContainer() { Collection = OtherTypeSources }
};
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.