【问题标题】:Access Controls in Silverlight DataGrid Column Header from Code代码中 Silverlight DataGrid 列标题中的访问控制
【发布时间】:2011-01-28 13:30:03
【问题描述】:

我们在 Silverlight DataGrid 中有使用 ContentTemplate 的自定义标题。我们在标题中有一个按钮,需要以编程方式访问该按钮以连接事件。我们正在使用 IronPython,因此我们无法在 xaml 中静态绑定事件(此外,我们将网格用于许多不同的视图 - 因此我们动态生成 xaml)。

我们如何才能访问数据网格列标题内的控件?

【问题讨论】:

    标签: .net silverlight datagrid ironpython header


    【解决方案1】:

    好的,所以我通过遍历网格的可视化树来寻找 DataColumnHeader - 然后遍历标题树并找到我们的按钮来解决它。

    遍历可视化树的代码:

    from System.Windows.Media import VisualTreeHelper
    
    def findChildren(parent, findType):
        count = VisualTreeHelper.GetChildrenCount(parent)
        for i in range(count):
            child = VisualTreeHelper.GetChild(parent, i)
            if isinstance(child, findType):
                yield child
            else:
                for entry in findChildren(child, findType):
                    yield entry
    

    它是这样称呼的:

    from System.Windows.Controls import Button
    from System.Windows.Controls.Primitives import DataGridColumnHeader
    
    for entry in findChildren(self._gridControl, DataGridColumnHeader):
        for button in findChildren(entry, Button):
            button.Click += handler
    

    请注意,调用此代码的方便时间是从 grid.Loaded 事件中调用,这可以确保已创建标题。

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 这看起来很有用。我们有对 DataGrid 实例的引用,我如何访问 ControlTemplate 以使用 FrameWorkElement.GetTemplateChild()?
      • 嗯...加上该方法受到保护。
      猜你喜欢
      • 2012-05-24
      • 1970-01-01
      • 2015-03-05
      • 2023-03-07
      • 2011-06-17
      • 2011-04-18
      • 2011-10-10
      • 2011-09-30
      • 1970-01-01
      相关资源
      最近更新 更多