如何从WPF中的DataGridCell中找到其所有者DataGrid和DataGridRow?

发布于 2024-09-26 06:08:35 字数 123 浏览 3 评论 0原文

在 DataGrid 命令的事件处理程序中,我在 ExecutedRoulatedEventArgs 中获取 DataGridCell。但是,我不知道如何获取其关联的 DataGrid 和 DataGridRow。非常感谢您的帮助。

In an event handler for a Command for a DataGrid, I get DataGridCell in ExecutedRoutedEventArgs. However, I couldn't figure out how to get its associated DataGrid and DataGridRow. Your help is much appreciated.

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

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

发布评论

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

评论(3

月亮坠入山谷 2024-10-03 06:08:35

您可能想要设置某种RelativeSource绑定,可以通过{RelativeSource FindAncestor, AncestorType={x:Type 获取“父网格/行” DataGrid}},但你的问题让我思考......

你可以:

使用反射:

var gridCell = ....;
var parentRow = gridCell
         .GetType()
         .GetProperty("RowOwner", 
               BindingFlags.NonPublic | BindingFlags.Instance)
         .GetValue(null) as DataGridRow;

使用VisualTreeHelper

var gridCell = ...;
var parent = VisualTreeHelper.GetParent(gridCell);
while(parent != null && parent.GetType() != typeof(DataGridRow))
{
    parent = VisualTreeHelper.GetParent(parent);
}

You probably want to set some sort of RelativeSource binding that can get you the "parent grid/row" via a {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, but your question got me thinking...

You could:

Use Reflection:

var gridCell = ....;
var parentRow = gridCell
         .GetType()
         .GetProperty("RowOwner", 
               BindingFlags.NonPublic | BindingFlags.Instance)
         .GetValue(null) as DataGridRow;

Use the VisualTreeHelper:

var gridCell = ...;
var parent = VisualTreeHelper.GetParent(gridCell);
while(parent != null && parent.GetType() != typeof(DataGridRow))
{
    parent = VisualTreeHelper.GetParent(parent);
}
缱绻入梦 2024-10-03 06:08:35

这是我认为完整的答案......

    private void Copy(object sender, ExecutedRoutedEventArgs e)
    {
        DataGrid grid = GetParent<DataGrid>(e.OriginalSource as DependencyObject);
        DataGridRow row = GetParent<DataGridRow>(e.OriginalSource as DependencyObject);
    }

    private T GetParent<T>(DependencyObject d) where T:class
    {
        while (d != null && !(d is T))
        {
            d = VisualTreeHelper.GetParent(d);
        }
        return d as T;
    }

Here is what I think is a complete answer...

    private void Copy(object sender, ExecutedRoutedEventArgs e)
    {
        DataGrid grid = GetParent<DataGrid>(e.OriginalSource as DependencyObject);
        DataGridRow row = GetParent<DataGridRow>(e.OriginalSource as DependencyObject);
    }

    private T GetParent<T>(DependencyObject d) where T:class
    {
        while (d != null && !(d is T))
        {
            d = VisualTreeHelper.GetParent(d);
        }
        return d as T;
    }
氛圍 2024-10-03 06:08:35

您可以做的一种方法是将一个或两个所需元素作为 CommandParameter 传递:

<MouseBinding 
    MouseAction="LeftDoubleClick" 
    Command="cmd:CustomCommands.Open" 
    CommandParameter="{Binding ElementName=MyDataGrid}}" />

如果您需要两者,您可以添加一个多值转换器,将它们组合成一个 Tuple (或保留它)作为对象[])

然后在您的代码隐藏中您可以使用 e.Parameter 访问它

One way you could do is by passing one or both of the needed elements in as a CommandParameter:

<MouseBinding 
    MouseAction="LeftDoubleClick" 
    Command="cmd:CustomCommands.Open" 
    CommandParameter="{Binding ElementName=MyDataGrid}}" />

If you need both, you could add a multi-value converter that combines them into a Tuple (or leave it as an object[])

Then in your code-behind you can access it by using e.Parameter

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