1

DataGridColumnから継承してカスタムを構築していますが、このエラーを出力ウィンドウに表示DataGridBoundColumnしようとするとDependencyProperty

System.Windows.Data エラー: 2: ターゲット要素の管理 FrameworkElement または FrameworkContentElement が見つかりません。BindingExpression:Path=myViewModelProperty; DataItem=null; ターゲット要素は 'CustomDataGridComboBoxColumn' (HashCode=24500989) です。ターゲット プロパティは 'ItemsSourcePath' (タイプ 'Object') です

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}"/>
4

0 に答える 0