我正在DataGridColumn
通过继承来构建自定义,DataGridBoundColumn
但是当我尝试使DependencyProperty
此错误出现在输出窗口中时
System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。绑定表达式:路径=myViewModelProperty;数据项=空;目标元素是“CustomDataGridComboBoxColumn”(HashCode=24500989);目标属性是“ItemsSourcePath”(类型“对象”)
我在 DataGridColumns 中阅读过有关 DataContext 的信息,并且听说此对象不属于可视树。但我找不到让他们从父级继承 DataContext 的方法
这是我使用的代码
public partial class CustomGridComboBoxColumn : DataGridBoundColumn
{
//The value indicating where to get the itemsSource
public static readonly DependencyProperty ItemsSourcePathProperty = DependencyProperty.Register(
"ItemsSourcePath",
typeof(string),
typeof(CustomGridComboBoxColumn));
}
/// <summary>
/// Gets or set the related collection
/// </summary>
public string ItemsSourcePath
{
get
{
return (string)GetValue(ItemsSourcePathProperty);
}
set
{
SetValue(ItemsSourcePathProperty, value);
}
}
然后我重写了创建编辑样式的方法
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
{
ComboBox comboBox = new ComboBox();
Binding binding = new Binding(ItemsSourcePath.ToString());
binding.Source = (this.DataGridOwner as DataGrid).DataContext;
BindingOperations.SetBinding(comboBox, ComboBox.ItemsSourceProperty,
binding);
return comboBox;
}
我正在尝试像这样使用它
<cc:CustomDataGridComboBoxColumn Header="New Column" ItemsSourcePath="{Binding myViewModelProperty}"/>