【问题标题】:How to access checkbox from datagrid template column?如何从数据网格模板列访问复选框?
【发布时间】:2014-01-29 14:54:47
【问题描述】:

好的,这已经被问过很多次了,但我就是找不到正确的解决方案。我有这样定义的数据网格:

<DataGrid AutoGenerateColumns="False"
      IsReadOnly="True"
      Name="InputDocItemsDataGrid"
      ItemsSource="{Binding Path= InputItems}" 
      SelectedItem="{Binding Path= InputItem, UpdateSourceTrigger=PropertyChanged}"
      SelectionChanged="InputDocItemsDataGrid_SelectionChanged"
      PreviewMouseLeftButtonDown="InputDocItemsDataGrid_PreviewMouseLeftButtonDown">
    <DataGrid.Columns>
        <DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
            <DataGridTemplateColumn.HeaderTemplate>
                <DataTemplate>
                    <CheckBox Name="cbxAll" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="cbxAll_Checked" />
                </DataTemplate>
            </DataGridTemplateColumn.HeaderTemplate>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Name="cbxSingleRow" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" PreviewMouseLeftButtonDown="cbxSingleRow_PreviewMouseLeftButtonDown" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

好吧,我在这里只添加了这个模板列,因为它是兴趣点。所以我要管理的是访问checkobx“cbxSingleRow”,因为它在数据网格之外,所以我可以用它做所有常规的事情,例如:cbxSingleRow.IsEnabled = false;

那么我如何获得那个复选框?

【问题讨论】:

  • “所有常规的东西”是通过 WPF 中的 DATABINDING 完成的。不是程序代码。
  • 我知道,我只需要从代码隐藏中访问该复选框,就像它是数据网格之外的任何其他复选框一样?有没有明确的解决方案来达到这种效果?
  • I just need to access that checkbox from codebehind - 有什么用?无论您想做什么,都可以通过 DataBinding 完成。
  • 嗨 Stojdza,,,,我面临同样的问题..你有解决方案吗?
  • @SANDEEP 干草!是的,我已经通过使用 Rohit 在他的回答中描述的可视化树帮助器类解决了这个问题,但只是有点不同。还有另一种方法可以通过数据绑定和使用 RelativeSource 属性来访问 DataGrid 内的控件。如果您需要更多信息,请告诉我。

标签: c# wpf datagrid datagridtemplatecolumn


【解决方案1】:

您可以在 VisualTreeHelper 类的帮助下获得它。

将此方法移动到某个实用程序类中以便可以重用。

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj,
                                         string name) where T : DependencyObject
{
    if (depObj != null)
    {
       for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
       {
          DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
          if (child != null && child is T &&
                (child as FrameworkElement).Name.Equals(name))
          {
             yield return (T)child;
          }

          foreach (T childOfChild in FindVisualChildren<T>(child, name))
          {
             yield return childOfChild;
          }
       }
    }
}

用法:

foreach (CheckBox checkBox in UtilityFunctions.
             FindVisualChildren<CheckBox>(InputDocItemsDataGrid, "cbxSingleRow"))
{           
   checkBox.IsChecked = true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    相关资源
    最近更新 更多