Replies: 1 comment 3 replies
-
You could give your node viewmodel the properties <TreeView
ItemsSource="{Binding Items}">
<TreeView.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Header}" />
</TreeDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerTheme>
<ControlTheme
x:DataType="viewModels:Node"
BasedOn="{StaticResource {x:Type TreeViewItem}}"
TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
</ControlTheme>
</TreeView.ItemContainerTheme>
</TreeView> |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm currently migrating a WPF application to Avalonia 11.x and got almost everything done. The one thing that's missing is the tree sync after selecting a search result in a flat list.
I basically got a
TreeView
with node view models bound to it, and aListBox
/TextBox
next to it that allow me to search for nodes. Selecting a result in the listbox should select the associated tree node, and expand the ancestors to make it fully visible (all based on the view model behind theTreeViewItem
).In WPF, I had to ask each levels
ItemsControl.ItemContainerGenerator
if itsStatus
is loaded already, and if not attach to itsStatusChanged
event to keep selecting once the expansion is complete and UI items are present. Inside this, I simply usedItemsControl.ItemContainerGenerator.ContainerFromIndex
to grab the associated container, setIsExpanded
and continue until I hit the desired item.In Avalonia, no such thing exists; it even refers me back from
ItemsControl.ItemContainerGenerator.ContainerFromIndex
to simply ask the parentItemsControl.ContainerFromIndex
. There's no expansion/visuals/control status, and I can't detect if and when a level is present, so I'm often stuck when the desired node is deeper in the tree (because the containers don't exist yet).I've tried a few variations of #13475, #11845 and #16485 but none of them really get me where I want to be.
(Don't really worry about my progress there, I might switch to a
TreeDataGrid
anyways, and do some other things to get this to work.)Then I stumbled over the remark on
TreeView.SelectedItem
:Avalonia/src/Avalonia.Controls/TreeView.cs
Lines 111 to 112 in 18fcfcc
And...well...what is even
Selection.SelectedIndex
? I know my indices per level, and I can keep track of the global index (in a flattened list), but I couldn't figure out what this meant. There is aAvalonia.Controls.Selection
namespace and aAvaloniaEdit.Editing.Selection
class, but none of those really help me. What seemed more promising wasAvalonia.Controls.Selection.ISelectionModel
(which has aSelectedIndex
property), but no connection to theTreeView
.To get to the point: Is that documentation comment outdated? Should it point us elsewhere, or is it obsolete and doesn't apply anymore?
With the information i currently have on this, I don't know how to proceed, and the only useful results I can find on Google is the source code comment and the documentation; so it feels like something that needs work (one way or another).
Beta Was this translation helpful? Give feedback.
All reactions