【问题标题】:How to get grip of a DataGrid's active RowDetails如何掌握 DataGrid 的活动 RowDetails
【发布时间】:2011-05-14 14:35:52
【问题描述】:

我有一个带有 RowDetailsTemplate 的 DataGrid,其中包含另一个网格。

我想对双击该 detailgrid 中的一行做出反应,并将单元格的内容填充到所选父行的相应单元格中。

<DataGrid Name="dataGrid1" DataContext="{Binding}" ItemsSource="{Binding Source={StaticResource ..}}" AutoGenerateColumns="False">
   <DataGrid.Columns>
     <DataGridTextColumn Header="Old Link Source" Binding="{Binding Path=OldLinkSource}"/>
     <DataGridTextColumn Header="New Link Source" Binding="{Binding Path=NewLinkSource}"/>
   </DataGrid.Columns>

   <DataGrid.RowDetailsTemplate>
     <DataTemplate>
       <DataGrid Name="dataGrid1Details" ItemsSource="{Binding Path=PossibleCandidates}" AutoGenerateColumns="False">
         <DataGrid.Columns>
           <DataGridTextColumn Header="Similarity" Binding="{Binding Path=Key}"/>
           <DataGridTextColumn Header="Possible New Link Source" Binding="{Binding Path=Value}"/>
         </DataGrid.Columns>
       </DataGrid>
     </DataTemplate>
   <DataGrid.RowDetailsTemplate>
</DataGrid>

据我了解,每次更改行时都会重新创建 detailgrid。我是 WPF 新手,对如何掌握当前可见的 detailgrid 并订阅其事件一无所知。

【问题讨论】:

    标签: wpf datagrid master-detail rowdetails


    【解决方案1】:

    您可以在 RowDetails DataGrid 中为 DataGridRow 添加 Style 并从那里订阅 MouseDoubleClick 事件。

    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <DataGrid Name="dataGrid1Details" ItemsSource="{Binding Path=PossibleCandidates}" AutoGenerateColumns="False">
                <DataGrid.Resources>
                    <Style TargetType="{x:Type DataGridRow}">
                        <EventSetter Event="MouseDoubleClick" Handler="DetailedDataGridRow_MouseDoubleClick"/>
                    </Style>
                </DataGrid.Resources>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Similarity" Binding="{Binding Path=Key}"/>
                    <DataGridTextColumn Header="Possible New Link Source" Binding="{Binding Path=Value}"/>
                </DataGrid.Columns>
            </DataGrid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
    

    代码隐藏,简单的 EventHandler

    // Fill cell data.. You can access the values like this
    void DetailedDataGridRow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        DataGridRow clickedDataGridRow = sender as DataGridRow;
        // Details: clickedDataGridRow.Item
        // Main DataGrid: dataGrid1.SelectedItem
    }
    

    更新

    在某种程度上,RowDetails 和 DataGridRow 是连接的。 RowDetails 在 VisualTree 的 DataGridRow 中,因此有很多方法可以访问它(事件、行走 VisualTree 等),但我认为没有属性或类似的东西可以让你直接访问(据我所知)。 Snoop 的屏幕截图显示 DataGridRow 中的 DataGridDetailsPresenter

    【讨论】:

    • @VVS:还是我误解了这个问题?
    • 我不得不承认我是和另一个项目一起移动的,并没有重新审视这个问题。我仍然不明白 DataGridRow 和 RowDetails 是如何连接的。如您的解决方案所示,是通过使用事件的“发送者”找出关联行的唯一方法吗?
    猜你喜欢
    • 2015-10-27
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2011-05-27
    • 2011-03-21
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多