2017-05-08 13 views
0

カスタムDataPropertyをDataGridTextColumnに追加しようとしました。WPF:カスタムDependencyPropertyへのバインドが不可能

I'vは、カスタムクラスを継承し、次のように依存関係プロパティを追加しました:

public class CustomDataGridTextColumn : DataGridTextColumn 
{ 
    public int MyProperty 
    { 
     get { return (int)GetValue(MyPropertyProperty); } 
     set { SetValue(MyPropertyProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty MyPropertyProperty = 
     DependencyProperty.Register("MyProperty", typeof(int), typeof(CustomDataGridTextColumn), new PropertyMetadata(0)); 


} 

私が)(右InitComponents後に私のメインウィンドウコンストラクタに次のコードでバインディングを設定し;:

CustomDataGridTextColumn test = new CustomDataGridTextColumn() 
{ 
    Header = "1. Operand", 
    Binding = new Binding("Operand1") //<- This works 
}; 

test.SetValue(CustomDataGridTextColumn.MyPropertyProperty, new Binding("Operand1")); // <- This doesn't 

私のアプリケーションを起動すると、 "System.Windows.Data.Binding"がプロパティ "MyProperty"の有効な値ではないことを示す "test.SetValue(...)"で "System.ArgumentExcpetion" : "CS1324"のようなエラーコードがないので、このエラーメッセージは私によって翻訳されます。

私の知る限り、すべての依存関係プロパティはデータバインディングをサポートする必要がありますか?

+0

を、私は、これは背後にあるコードで行わなければならないことを言及するのを忘れてしまいました。 –

答えて

2

の背後にあるコードでバインディングを確立するためには、あなたの代わりにSetValueBindingOperations.SetBinding方法を使用する必要があります:

BindingOperations.SetBinding(
    test, 
    CustomDataGridTextColumn.MyPropertyProperty, 
    new Binding("Operand1")); 
+0

不幸DataGridTextColumnのSetBindingメソッドがありません –

+0

編集を参照してください.FrameworkElementから派生したクラスは便宜上[SetBinding](https://msdn.microsoft.com/en-us/library/ms598270.aspx)メソッドを提供しています。 – Clemens

+0

これは私のために今働きます。ありがとう。良い一日を。 –

関連する問題