FlyingStreudel
FlyingStreudel

Reputation: 4464

WPF Databinding Treeview to List does not update

I have a List bound to a TreeView like:

XAML:

<TreeView Name="browserTree" 
          BorderBrush="DarkSlateGray"
          BorderThickness="1"
          Grid.Row="2"
          Margin="0,3,0,0"
          ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}">
    <TreeView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

C#:

browserTree.DataContext = treeList;

I update the list via:

void QueryChange(string s)
{
    rCM.SetCommand(s);
    treeList.Clear();
    SqlDataReader sr = rCM.ExecuteReader(System.Data.CommandBehavior.Default);
    while (sr.Read())
    {
        treeList.Add((string)sr["tree_hdr"]);
    }
    sr.Close();
}

The List<string> is just a placeholder at the moment for a more meaningful data class I have yet to implement. But right now I need to know why the TreeView is not updating to reflect the changes made to the list.

Upvotes: 0

Views: 1362

Answers (2)

Jobi Joy
Jobi Joy

Reputation: 50038

Please check the type of your treeList which you set as DataContext. It has to be an ObservableCollection to reflect your collection changes in the UI

Or else for quick workaround, just set the DataContext again after you filled the List.

Upvotes: 0

gmn
gmn

Reputation: 4319

Try making the treelist an ObservableCollection.

Upvotes: 3

Related Questions