我已经使用Josh Smith's MVVM model创建了一个树视图,但是我想创建一个相邻的数据网格,该数据网格将更改以表示树视图中选定的组件。
但是,我不知道如何将数据网格的项源绑定到选定的treeviewitem,因为我无法访问在treegrid中选定的组件。
<Window x:Class="TreeDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TreeDataGrid"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TreeView Name = "myTreeView" ItemsSource="{Binding FirstGeneration}" HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="300">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" ItemsSource="{Binding SelectedComponent, UpdateSourceTrigger=PropertyChanged}">
</DataGrid>
</Grid>
</Window>
字符串
是我的XAML。
namespace TreeDataGrid
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
readonly AllComponentsTreeViewModel allComponentsTreeViewModel;
public MainWindow()
{
InitializeComponent();
//Get the sample components data
Component demoComponent = new Component();
Component demoChild1 = new Component();
Component demoChild2 = new Component();
Component demoSubChild = new Component();
demoComponent.Name = "RootBoy";
demoChild1.Name = "Child1";
demoChild2.Name = "Child2";
demoSubChild.Name = "SubChild";
DataGridProps data1 = new DataGridProps(23, "demo", false);
DataGridProps data2 = new DataGridProps(25, "is", true);
demoComponent.Data = new List<Object>() { data2, data2 };
demoChild1.Data = new List<Object>() { data1, data2 };
demoChild2.Data = new List<Object>() { data1, data2 };
demoSubChild.Data = new List<Object>() { data2, data1 };
demoChild1.SubComponents = new List<Component>() { demoSubChild };
demoComponent.SubComponents = new List<Component>(){ demoChild1, demoChild2 };
allComponentsTreeViewModel = new AllComponentsTreeViewModel(demoComponent);
base.DataContext = allComponentsTreeViewModel;
}
}
}
型
我的主要窗口我在allComponentsTreeViewModel中有一个SelectedComponent getter,但我不知道如何检索所选的TreeViewItem,更重要的是,当选择一个新的TreeViewItem时,如何更新SelectedComponent。Here's a mockup of what I want, with a different data grid when a new treeviewitem is selected.我这样做是正确的吗?
1条答案
按热度按时间kyvafyod1#
在TreeView中,应绑定SelectedItem属性。
字符串
然后将其用作DataGrid中的ItemsSource
型
在ViewModel中,当SelectedItemInTreeView更改时,应引发PropertyChangedEvent,以便在TreeView中的选择更改时刷新DataGrid ItemsSource。