我不明白为什么我的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; }
}
https://stackoverflow.com/questions/51305885
复制相似问题