将树视图中的选定项目链接到数据网格

问题描述 投票:0回答:0

我使用 Josh Smith 的 MVVM 模型创建了一个树视图,但我想创建一个相邻的数据网格,该数据网格会更改以表示在树视图中选择的组件。

但是,我无法弄清楚如何将数据网格的项目源绑定到选定的树视图项目,因为我无法访问在树形网格中选择的组件。

<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. 我以正确的方式处理这个问题吗?

c# .net wpf datagrid treeview
© www.soinside.com 2019 - 2024. All rights reserved.