Here’s a really simple bit of xaml:
<TreeView x:Name="provenanceTree"
ItemsSource="{x:Bind ProvenanceTreeItems}" SelectionMode="Single"
SelectedItem="{x:Bind focus, Mode=TwoWay}"
>
<!-- Definitions in here -->
</TreeView>
The idea here is that there’s a focus
for the control, and the treeview’s selection should follow that focus.
Mostly, it works. But the problem comes about when a change of focus requires additions to the ItemsSource
– that is: I’m trying to both add an item and select it in the same operation. Naturally, the TreeView redraws, but nothing gets selected – if anything was selected previously it gets de-selected.
I tried assigning the SelectedItem
not in a bindng, but in the refocussing code, but I hit the same problem.
I get it: changes to the ItemsSource
don’t get passed to the TreeView right away, so I’m trying to select something that’s not in the tree. How can I tell when it is safe to select a newly-added item? And is there any way to adust the binding so it works as intended?