c# - ObservableCollection 不更新 TreeView
问题描述
我不明白为什么我的 TreeView 不使用 ObservableCollection 更新。
主窗口
public ObservableCollection<Student> listStudents { get; set; }
internal MainWindow(List<Student> list, Controller controller)
{
this.listStudents = new ObservableCollection<Student>(list);
this.controller = controller;
InitializeComponent();
this.DataContext = this;
}
XAML
<TreeView x:Name="tv_source" AllowDrop="True" Grid.Row="2" Grid.Column="1" Margin="0,5" HorizontalAlignment="Stretch" ItemsSource="{Binding ListStudents}" Background="White" BorderBrush="{x:Null}">
我添加一个这样的孩子:
public void addChild(TreeViewItem _sourceItem, TreeViewItem _targetItem)
{
Dispatcher.BeginInvoke(new Action(() => ((Student)_targetItem.DataContext).Phones.Add(_sourceItem.DataContext.ToString())));
}
学生班
public class Student
{
public string Name { get; set; }
public List<string> Phones { get; set; }
}
解决方案
如果要将字符串添加到Phones
属性并针对此集合绑定数据,则应将其类型更改ObservableCollection<string>
为添加string
以显示在 UI 中。现在好像是一个List<string>
。
推荐阅读
- api - REVOLUT 业务 api 支付在 2 个账户之间失败
- python - 如何在python中读取文件扩展名为.accdb的Ms access DB表?
- python - 图像的像素坐标:访问图像位置的方法
- mongodb - MongoDB / Mongoose查询以查找另一个文档中是否存在ID,如果存在则在结果中添加一个标志
- c# - 如何将 Castle.windsor 从版本 4 升级到版本 5.0.1
- python - Python - 导入模型
- rust - 如何使用功能标志明智地执行“货物构建”命令功能?
- angular - PrimeNG 中的多个样式类
- html - 有没有办法使用 Materialize.css 只指定一次背景颜色?
- javascript - TypeError:无法获取 REACT JS