1

I don't understand why my TreeView not update using a ObservableCollection.

Main 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}">

I add a child like this :

public void addChild(TreeViewItem _sourceItem, TreeViewItem _targetItem)
        {
            Dispatcher.BeginInvoke(new Action(() => ((Student)_targetItem.DataContext).Phones.Add(_sourceItem.DataContext.ToString())));
        }

Student class

public class Student
{ 
    public string Name { get; set; }

    public List<string> Phones { get; set; } 
}
Qwertyuiop
  • 13
  • 4
  • 1
    If you are adding a `string` to the `Phones` property and data bind against this collection, you should change its type to `ObservableCollection` for the added `string` to show up in the UI. Now it seems to be a `List`. – mm8 Jul 12 '18 at 12:44
  • 1
    Is ListStudents defined with starting letter in lower case (listStudents) or is just a typing error? -> ItemsSource="{Binding ListStudents}" inside the class is defined public ObservableCollection listStudents { get; set; } – Babbillumpa Jul 12 '18 at 12:48
  • @mm8 thank you, it works – Qwertyuiop Jul 12 '18 at 14:48

1 Answers1

0

If you are adding a string to the Phones property and data bind against this collection, you should change its type to ObservableCollection<string> for the added string to show up in the UI. Now it seems to be a List<string>.

mm8
  • 163,881
  • 10
  • 57
  • 88