【发布时间】: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