当我尝试创建DependencyProperty时:找不到管理FrameworkElement或FrameworkContentElement

时间:2013-01-29 23:28:50

标签: mvvm datagridcomboboxcolumn

我正在通过继承DataGridColumn来构建自定义DataGridBoundColumn,但当我尝试制作DependencyProperty此错误会出现在输出窗口中

  

System.Windows.Data错误:2:找不到管理FrameworkElement   或目标元素的FrameworkContentElement。   BindingExpression:路径= myViewModelProperty;的DataItem = NULL;目标元素是   'CustomDataGridComboBoxColumn'(HashCode = 24500989);目标属性是   'ItemsSourcePath'(输入'Object')

我在DataGridColumns中读过有关DataContext的内容,我听说过这个对象不属于Visual Tree。但我找不到让他们从父

继承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}"/>

0 个答案:

没有答案