Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a TreeView, which is bound to a hierarchical data structure in my view-model in the usual MVVM manner, something like this:

VIEW-MODEL

This is constructed when the view (System.Windows.Window) is constructed and set as the DataContext for the lifetime of the Window.

public class MyViewModel : INotifyPropertyChanged
{
public List<TreeNode> Nodes
{
	get => _nodes;
}

private List<TreeNode> _nodes;
}


VIEW

<TreeView
    Visibility="{Binding EvaluateTree,Converter={StaticResource BooleanVisibleCollapsedNegationConverterKey}}"
    ItemsSource="{Binding Nodes}">
</TreeView>

Everything works as expected, except that when I want to force an update in the UI, I can’t get it to do it.

What I have tried:

I try to force the update by calling a function like this:

public void ForceUpdate()
{
	_nodes = RebuildNodes();
	OnPropertyChanged( nameof(Nodes) );
}

If I place a breakpoint in the ForceUpdate() function, it is hit, but a breakpoint in the get for the Nodes is not hit, implying UI binding engine is not reading the updated list.

I must be doing something obvious wrong. Can anyone tell me what?

One final thought...

The TreeView is displaying the folders on a physical drive, like a mini-Explorer. When I force the update, sometimes the folders will have changed but often they won't. Does TreeView maybe keep a hashed value of the nodes to determine if it really needs to update or not? I think I am clutching at straws here because I set the data-source to null so it's going to get garbage collected and I don't see how the UI can continue to use it.
Posted
Updated 14-Aug-21 20:19pm
v4
Comments
Richard Deeming 11-Aug-21 9:54am    
What does the WPF markup for the TreeView look like?
Patrick Skelton 12-Aug-21 2:59am    
I've added it, plus a long-shot explanation of what might be happening.
[no name] 11-Aug-21 11:33am    
(Re)setting the DataContext is one way; if you're using the usual binding.
Patrick Skelton 12-Aug-21 2:58am    
You mean for the whole screen?
[no name] 13-Aug-21 14:23pm    
I have no idea where you set the DataContext, how many bindings you have, or why you think anyone might notice. Usually, one tries and reports their experience.

this.DataContext = this;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900