我正在使用 WPF 工具包中的 WPF DataGrid
。
我向 DataGrid
添加了一个模板化列,其中每个单元格中都有一个 CheckBox
。现在我如何访问这些单元格中的值?
DataGrid
中的其他列来自 DataSet
。我可以访问这些,但无法获取添加到 DataGrid
的 DataGridTemplateColumn
的值。
大家有什么想法吗?
最佳答案
您现在开始从视觉树中提取内容。这是一项艰苦的工作,您找不到绑定(bind),因为它被埋在单元格模板中。我所做的就是为此类内容添加我自己的列,该列派生自 DataGridBoundColumn,这意味着它具有像所有其他列一样的绑定(bind):(我不久前写过它,它可能需要一些查看)这让我只使用直装订。我不必设置单元格模板,我可以使用我更喜欢的 DataTemplate。
public class DataGridReadOnlyObjectDisplayColumn : DataGridBoundColumn {
public DataGridReadOnlyObjectDisplayColumn() {
//set as read only,
this.IsReadOnly = true;
}
/// <summary>
/// Gets and Sets the Cell Template for this column
/// </summary>
public DataTemplate CellTemplate {
get { return (DataTemplate)GetValue(CellTemplateProperty); }
set { SetValue(CellTemplateProperty, value); }
}
// Using a DependencyProperty as the backing store for CellTemplate. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CellTemplateProperty =
DependencyProperty.Register("CellTemplate", typeof(DataTemplate), typeof(DataGridReadOnlyObjectDisplayColumn), new UIPropertyMetadata(null));
protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem) {
//create the simple field text block
ContentControl contentControl = new ContentControl();
contentControl.Focusable = false;
//if we have a cell template use it
if (this.CellTemplate != null) {
contentControl.SetValue(ContentControl.ContentTemplateProperty, this.CellTemplate);
}
//set the binding
ApplyBinding(contentControl, ContentPresenter.ContentProperty);
//return the text block
return contentControl;
}
/// <summary>
/// Assigns the Binding to the desired property on the target object.
/// </summary>
internal void ApplyBinding(DependencyObject target, DependencyProperty property) {
BindingBase binding = Binding;
if (binding != null) {
BindingOperations.SetBinding(target, property, binding);
}
else {
BindingOperations.ClearBinding(target, property);
}
}
protected override System.Windows.FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) {
//item never goes into edit mode it is a read only column
return GenerateElement(cell, dataItem);
}
}
现在,如果您可以到达该列,您就可以到达该列上的绑定(bind)。如果您可以到达该单元格,那么您就可以找到数据项(行数据)。然后我要做的就是按照绑定(bind)来获取单元格值。它确实效率低下,而且是一种 hack。但它有效。为了遵循绑定(bind),我使用了这个。
private Object GetCellValue(Binding columnBinding, object dataSource) {
Object valueField = null;
if (columnBinding != null) {
BindingEvaluator bindingEvaluator = new BindingEvaluator();
//copy the binding
Binding binding = new Binding();
binding.Path = columnBinding.Path;
binding.Source = dataSource;
//apply the binding
BindingOperations.SetBinding(bindingEvaluator, BindingEvaluator.BindingValueProperty, binding);
//get the current cell item
valueField = bindingEvaluator.BindingValue as IValueField;
}
return valueField;
}
最后一个是一个名为 BindingEvaluator 的辅助类,它有一个 dp,我用它来跟踪绑定(bind)
public class BindingEvaluator : DependencyObject {
/// <summary>
/// Gets and Sets the binding value
/// </summary>
public Object BindingValue {
get { return (Object)GetValue(BindingValueProperty); }
set { SetValue(BindingValueProperty, value); }
}
// Using a DependencyProperty as the backing store for BindingValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BindingValueProperty =
DependencyProperty.Register("BindingValue", typeof(Object), typeof(BindingEvaluator), new UIPropertyMetadata(null));
}
我这样调用它:
var valueField = this.GetCellValue(column.Binding as Binding, datagrid.CurrentCell.Item);
关于c# - DataGridTemplateColumn 获取单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1936732/