K
K
kolenchits862014-12-28 11:18:57
WPF
kolenchits86, 2014-12-28 11:18:57

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>

plus the difficulty is that each child node is an instance of a different class than the parent, but I think it's not a problem.
I just can't find a solution on how to bind the HierarchicalDataTemplate variable on click

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Papin, 2015-01-06
@kolenchits86

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>
...

V
Vasily Samoilov, 2015-01-04
@Imagio

And if you subscribe to the TreeView.SelectedItemChanged event:

private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
            selectedModel = e.NewValue;
}

And then you can already determine the type of the selected element.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question