Create a Master / Details View Based on a TreeView Control in Silverlight

I have a TreeNode class with the following properties

 public string Name { get; set; }
 public string Description { get; set; }
 public bool AllowMultiples { get; set; }
 private ObservableCollection<TreeNode> _childNodes = new ObservableCollection<TreeNode>();

In my user interface, I want to display the tree in the left pane and the individual elements in the right.

My XAML is as follows:

<UserControl.Resources>
    <win:HierarchicalDataTemplate x:Key="ChildTemplate" ItemsSource="{Binding ChildNodes}">
        <TextBlock FontStyle="Italic" Text="{Binding Path=Name}" />
    </win:HierarchicalDataTemplate>        
    <win:HierarchicalDataTemplate x:Key="NameTemplate" 
    ItemsSource="{Binding Path=ChildNodes}" 
    ItemTemplate="{StaticResource ChildTemplate}">
        <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" />
    </win:HierarchicalDataTemplate>        
</UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="20"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
     </Grid.ColumnDefinitions>
    <TextBlock Text="Document Hierarchy" Margin="0,0,43,0" FontSize="13" />
    <toolkit:TreeViewDragDropTarget Grid.Row="1" Grid.Column="0" BorderThickness="-1">
        <controls:TreeView BorderThickness="0" ItemTemplate="{StaticResource ChildTemplate}" x:Name="treeView" ItemsSource="{Binding}">                
        </controls:TreeView>
    </toolkit:TreeViewDragDropTarget>
    <TextBlock Text="Properties"  FontSize="11" Grid.Column="1" Grid.Row="0" />
    <Grid Grid.Row="1" Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" ></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>                
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock x:Name="AlloMultPropLabel" Text="Allow Multiple" Grid.Column="0" Grid.Row="0"></TextBlock>
        <RadioButton x:Name="AllowMulti" GroupName="GrpAllowMulti" Content="True" Grid.Column="1" Grid.Row="0"/>
        <RadioButton x:Name="AllowMulti2" GroupName="GrpAllowMulti" Content="False"  Grid.Column="2" Grid.Row="0" IsChecked="True" />
        <TextBlock x:Name="DescPropLabel" Text="Description" HorizontalAlignment="Left" Width="74"  Grid.Row="2" ></TextBlock>
        <TextBox x:Name="DescProp" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Text="{Binding Description}"  />           
    </Grid>

Using http://www.silverlight.net/learn/quickstarts/bindingtocontrols/ as my link, I set LayoutRoot.DataContext to CollectionViewSource, as shown below:

public ObservableCollection<TreeNode> itemsSource =   new ObservableCollection<TreeNode>()
    {
      new TreeNode("Root", "ths is test"),
      new TreeNode("Secondary","Second node"),
};


    public MainPage()
    {
        InitializeComponent();
        //treeView.DataContext = itemsSource;
        LayoutRoot.DataContext = new CollectionViewSource { Source = itemsSource };

    }

When I start this project, I notice that the description is always set to describe the first elements. It does not change depending on the choice.

Any pointers to get this right?

+3
source share
1 answer

TextBox "{Binding Description}". DataContext, TreeView, .

, SelectedItem Treeview. "Detail" Grid , DataContext: -

<Grid DataContext="{Binding SelectedItem, ElementName=treeView}">
   <Grid.ColumnDefinitions>
      <Grid.ColumnDefinition Width="Auto" />
      <Grid.ColumnDefinition Width="*" />
   </Grid.ColumnDefinitions>
   <Grid.RowDefinitions>
      <Grid.RowDefinition Height="Auto" />
      <Grid.RowDefinition Height="Auto" />
   </Grid.RowDefinitions>

   <TextBlock Text="Name" Grid.Row="0" Grid.Column="0" />
   <TextBox Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>

   <TextBlock Text="Description" Grid.Row="1" Grid.Column="0" />
   <TextBox Text="{Binding Description}" Grid.Row="1" Grid.Column="1"/>
</Grid>

Grid DataContext, treeView SelectedItem.

+1

Source: https://habr.com/ru/post/1783822/


All Articles