ObservableCollection 不更新 TreeView
ObservableCollection not update TreeView
我不明白为什么我的 TreeView 不使用 ObservableCollection 进行更新。
主要Window
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}">
我这样添加 child :
public void addChild(TreeViewItem _sourceItem, TreeViewItem _targetItem)
{
Dispatcher.BeginInvoke(new Action(() => ((Student)_targetItem.DataContext).Phones.Add(_sourceItem.DataContext.ToString())));
}
学生class
public class Student
{
public string Name { get; set; }
public List<string> Phones { get; set; }
}
如果要将字符串添加到 Phones
属性 并针对此集合进行数据绑定,则应将其类型更改为 ObservableCollection<string>
,以便将 string
添加到出现在 UI 中。现在好像是List<string>
.
我不明白为什么我的 TreeView 不使用 ObservableCollection 进行更新。
主要Window
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}">
我这样添加 child :
public void addChild(TreeViewItem _sourceItem, TreeViewItem _targetItem)
{
Dispatcher.BeginInvoke(new Action(() => ((Student)_targetItem.DataContext).Phones.Add(_sourceItem.DataContext.ToString())));
}
学生class
public class Student
{
public string Name { get; set; }
public List<string> Phones { get; set; }
}
如果要将字符串添加到 Phones
属性 并针对此集合进行数据绑定,则应将其类型更改为 ObservableCollection<string>
,以便将 string
添加到出现在 UI 中。现在好像是List<string>
.