我正在使用WPF Toolkit中的WPF DataGrid
。DataGridTemplateColumn获取单元格的值
我在我的DataGrid
中添加了一个模板列,它在每个单元中有一个CheckBox
。现在我该如何访问这些单元格中的值?
DataGrid
我的其他专栏来自DataSet
。我可以访问这些,但我无法获得DataGridTemplateColumn
的值,我将其添加到DataGrid
。
任何人有任何想法?
我正在使用WPF Toolkit中的WPF DataGrid
。DataGridTemplateColumn获取单元格的值
我在我的DataGrid
中添加了一个模板列,它在每个单元中有一个CheckBox
。现在我该如何访问这些单元格中的值?
DataGrid
我的其他专栏来自DataSet
。我可以访问这些,但我无法获得DataGridTemplateColumn
的值,我将其添加到DataGrid
。
任何人有任何想法?
你现在把东西拉出视觉树。这就是艰苦的工作,你不能找到绑定,因为它被埋在了单元格模板中。我所做的是为这种东西添加我自己的专栏,这个专栏是从DataGridBoundColumn派生的,这意味着它与所有其他专栏一样具有绑定功能:(我前一段时间写过,它可能会在某些方面看到)我只是使用直接绑定。我没有设置单元格模板,我可以使用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);
}
}
现在,如果您可以到达列,您可以进入列的绑定。如果你可以到达单元格,那么你可以找到数据项目(行数据)。那么我所做的就是遵循绑定来获取单元格的值。它真的是效率低下,而且它是一个黑客。但它的工作原理。遵循绑定我使用这个。
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,我使用遵循结合
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);
我您的解决方案存在问题。我想使用DataGridTemplateColumn。但是,正如你所建议的那样,细胞的价值被埋在细胞模板之下。所以,你创建了你自己的控件直接使用DataTemplate。然后如果我尝试使用您的模板,那么当单元格正在编辑时如何获得不同的控件,因为您可能没有CellEditingTemplate。 – Vishal 2014-07-12 20:48:11
@Vishal对不起,我不记得,太久以前,我不再做WPF了。 – 2014-07-18 04:23:32