Answer the question
In order to leave comments, you need to log in
Binding Selected TreeNode how to do it with dynamic binding?
Hello!
I've been breaking my head all evening - I can't figure out how to bind the selected TreeView segment to the Mvvm variable, the
entire TreeView is here
<TreeView ItemsSource="{Binding Subunits}">
<!-- Шаблон подразделения -->
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}">
<TextBlock Text="{Binding SubunitName}"/>
<!-- Шаблон должности -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}" DataType="{x:Type local:Employee}">
<TextBlock Text="{Binding PositionName}"/>
<!-- Шаблон сотрудника -->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}" Margin="0,0,5,0" MouseDown="qwe"/>
<TextBlock Text="{Binding FirstName}" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Patronymic}" Margin="0,0,5,0"/>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Answer the question
In order to leave comments, you need to log in
Set a name (for example, MyControl) to an element with the desired context (in your case, the desired HierarchicalDataTemplate) and associate it with the SelectedItem field in the model through it
<Grid Name="LayoutRoot">
<Grid.DataContext>
<MyModel SelectedItem={Binding DataContext, ElementName=MyControl}>
</Grid.DataContext>
...
And if you subscribe to the TreeView.SelectedItemChanged event:
private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
selectedModel = e.NewValue;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question