【问题标题】:How to get a control inside a wpf datagrid如何在 wpf 数据网格中获取控件
【发布时间】:2013-01-28 06:46:42
【问题描述】:

我有一个包含多行的数据网格。在每一行内,第一列是一个按钮。我有行索引。假设我的行索引是 7。现在我想获取第 7 行内的按钮并更改其内容。

如何在数据网格的特定行中获取此按钮控件并更改其值?

【问题讨论】:

  • 你试过了吗?你能把这些代码贴在这里吗?

标签: wpf silverlight wpfdatagrid


【解决方案1】:

也许,下面的代码可以解决你的问题。

<DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Click="ShowHideDetails">Details</Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>

在 Codebehind C# 中访问控件

private void ShowHideDetails(object sender, RoutedEventArgs e)
        {
            // You can access the button and do whateve the changes you want
            Button objMyButton = null;
            if (sender is Button)
            {
                objMyButton = (sender as Button);

            }

            //You can access the parent object which means corresponding DataGridRow and do whatever you want

            for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
                if (vis is DataGridRow)
                {
                    var row = (DataGridRow)vis;                 
                    break;
                }
        }

【讨论】:

    猜你喜欢
    • 2011-11-11
    • 2016-08-30
    • 2021-11-08
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多