DataGrid
WPF Toolkitの WPF を使用しています。
テンプレート化された列を に追加しました。各セルにDataGrid
は があります。CheckBox
これらのセル内の値にアクセスするにはどうすればよいでしょうか?
の他の列DataGrid
はDataSet
. DataGridTemplateColumn
これらにアクセスできますが、に追加したの値にアクセスできませんDataGrid
。
誰にもアイデアはありますか?
DataGrid
WPF Toolkitの WPF を使用しています。
テンプレート化された列を に追加しました。各セルに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 と呼ばれるヘルパー クラスであり、1 つの 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);