C# WPF TreeView如何在添加/删除节点后刷新树?
我指的是这篇文章: 并修改树结构,如下所示:C# WPF TreeView如何在添加/删除节点后刷新树?,c#,wpf,xaml,treeview,hierarchicaldatatemplate,C#,Wpf,Xaml,Treeview,Hierarchicaldatatemplate,我指的是这篇文章: 并修改树结构,如下所示: Root |__Group |_Entry |_Source 在Entry.cs中: public class Entry { public int Key { get; set; } public string Name { get; set; } public ObservableCollection<Source> Sources { get; set; }
Root
|__Group
|_Entry
|_Source
在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;
}
}
}
在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>
在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);
}
公共ObservableCollection根=新ObservableCollection();
公共用户控制6()
{
初始化组件();
//…手动添加新节点
TreeView.ItemsSource=根;
}
私有void Add(对象发送方,ExecutedRoutedEventArgs e)
{
Entry ee=(Entry)TreeView.SelectedItem;
sources3=newsource(){Key=3,Name=“newsource”};
ee.来源。添加(s3);
}
当我在特定节点“条目”上单击右键以在条目下添加新节点“源”时
(调用“Add”方法),我成功地在条目下添加了一个新的“Source”对象,但在treeview上看不到这个新节点。如何在添加/删除节点时刷新treeview?如果要通知用户界面集合中的某些内容已更改,请使用而不是IList。就我而言,将
项的类型更改为ObservableCollection
不会解决问题。您需要实现INotifyPropertyChanged
。
我在树视图中测试了这两种解决方案,因为我面临相同的问题。
在我的例子中,将类型从IList更改为ObservableCollection并没有刷新GUI。但是,当我更改汽车属性时:
public List<SourceControlItemViewBaseModel> Items { get; set; }
在我使用的视图模型中:
currentVm.Items= await SourceControlRepository.Instance.BuildSourceControlStructureAsync(currentVm.ServerPath);
这意味着我没有添加/删除项目,但重建了节点的子集合。使用此类,源集合中的任何更改都将更新/刷新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 }
};
}
}
}
公共类条目
{
公共int密钥{get;set;}
公共字符串名称{get;set;}
公共ObservableCollection源{get;set;}
公众进入()
{
Sources=新的ObservableCollection();
}
公共合成集合项目
{
得到
{
返回新的CompositeCollection()
{
新建CollectionContainer(){Collection=Sources},
//在复合集合中添加其他类型的集合
//新建CollectionContainer(){Collection=OtherTypeSources}
};
}
}
}
好的!我将所有IList和List更改为“ObservableCollection”。接下来,如何通知用户界面集合中的某些内容已更改?(对不起,我是WPF新手)@user610801,只要将集合类型更改为ObservableCollection,我的WPF树视图就足以在我从集合中添加/删除项目时进行更新。
<TreeView ItemsSource="{Binding SourceControlStructureItems}" />
currentVm.Items= await SourceControlRepository.Instance.BuildSourceControlStructureAsync(currentVm.ServerPath);
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 }
};
}
}
}