我有一个具有以下属性的TreeNode类
public string Name { get; set; }
public string Description { get; set; }
public bool AllowMultiples { get; set; }
private ObservableCollection<TreeNode> _childNodes = new ObservableCollection<TreeNode>();
在我的UI中,我想在左侧面板中显示树,在右侧显示所选项目详细信息。
我的XAML如下:
<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>
使用http://www.silverlight.net/learn/quickstarts/bindingtocontrols/作为参考我已将LayoutRoot.DataContext设置为CollectionViewSource,如下所示:
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 };
}
当我运行这个项目时,我注意到描述始终设置为第一项描述。根据选择不会改变。
有任何指示要做到这一点吗?
答案 0 :(得分:1)
您已将TextBox绑定到“{Binding Description}”。它将使用TreeView启动的相同DataContext,因此它显示了顶部项的描述。
为了使文本框跟随树中的选定项,其数据上下文需要绑定到Treeview的SelectedItem
。我倾向于将“细节”显示放在自己的网格中并在那里管理它的布局,这将是更新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
。