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.