如何掌握 DataGrid 的活动 RowDetails

发布于 2024-10-04 04:23:41 字数 1143 浏览 10 评论 0原文

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

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

<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>

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

I have a DataGrid with a RowDetailsTemplate containing another grid.

I want to react on a doubleclick on a row in that detailgrid and fill the cell's content into a corresponding cell of the selected parent row.

<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>

From my understanding the detailgrid is recreated every time the row is changed. I'm new to WPF and clueless on how to get grip of the currently visible detailgrid and subscribe to its events.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2024-10-11 04:23:41

您可以在 RowDetails DataGrid 内添加 DataGridRow 的样式,并从那里订阅 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
}

Update

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

alt text

You can add a Style for DataGridRow inside the RowDetails DataGrid and subscribe to the MouseDoubleClick event from there.

<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>

Code behind, simple 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
}

Update

The RowDetails and the DataGridRow are connected, sort of. The RowDetails is in the DataGridRow in the VisualTree so there's many ways to access it (events, walking VisualTree etc.) but I don't think there's a Property or something like that that'll give you direct access (as far as I know). Screenshot from Snoop showing the DataGridDetailsPresenter in a DataGridRow

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文