简体   繁体   中英

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.

尝试使树形列表成为ObservableCollection。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM