简体   繁体   English

如何从datagrid模板列访问复选框?

[英]How to access checkbox from datagrid template column?

Ok this has been asked many times but I just can't find the right solution. 好的,这已经被问过很多遍了,但是我只是找不到正确的解决方案。 I have datagrid defined like this: 我有这样定义的数据网格:

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

Well I added only this template column here becouse it's the point of interest. 好吧,在这里我只添加了此模板列,因为这是我们的关注点。 So what I'm trying to manage is to access checkobx "cbxSingleRow" as it is outside of datagrid, so i would be able to do all the regular stuff with it like for example: cbxSingleRow.IsEnabled = false; 因此,我要管理的是访问checkobx“ cbxSingleRow”,因为它不在datagrid之外,因此我可以使用它来执行所有常规操作,例如:cbxSingleRow.IsEnabled = false;

So how do I get that checkbox? 那么如何获得该复选框?

You can get that with the help of VisualTreeHelper class. 您可以借助VisualTreeHelper类获得该信息。

Move this method in some utility class so that can be reused. 在某些实用工具类中移动此方法,以便可以重用。

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

Usage: 用法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM