2017-05-08 1 views
0

DataGridTextColumn에 사용자 지정 DataProperty를 추가하려고합니다.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 
후 내 MainWindow를 생성자에 다음 코드와 바인딩 설정

내 응용 프로그램을 시작할 때 ""System.Windows.Data.Binding "속성"MyProperty ""에 대한 유효한 값이 아닙니다 "("참고 "test.SetValue (...)"에서 "System.ArgumentExcpetion" : "CS1324"와 같은 오류 코드가 없으므로이 오류 메시지는 나에 의해 번역됩니다.

필자는 모든 종속성 속성이 데이터 바인딩을 지원해야한다고 생각합니까? 비하인드 코드에서 바인딩을 설정하기 위해

+0

내가이 뒤에 코드에서 수행해야 함을 언급하는 것을 잊었다. –

답변

2

, 당신은 SetValue 대신 BindingOperations.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

이것은 지금 저에게 좋습니다. 감사합니다. 좋은 하루 되세요. –

관련 문제