2009-10-28 103 views
0

我想克服一個限制,不允許我綁定到常規的clr屬性。WPF依賴屬性不確認

我使用的解決方案使用自定義依賴屬性,依次更改clr屬性。

下面是代碼

class BindableTextBox : TextBox 
{ 
    public static readonly DependencyProperty BoundSelectionStartProperty = DependencyProperty.Register("BoundSelctionStart", typeof(int), typeof(BindableTextBox), 
                              new PropertyMetadata(new PropertyChangedCallback(BindableTextBox.onBoundSelectionStartChanged))); 

    private static void onBoundSelectionStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     ((TextBox)d).SelectionStart = (int)e.NewValue; 
    } 

    private static readonly DependencyProperty BoundSelectionLenghtProperty = DependencyProperty.Register("BoundSelectionLenght", typeof(int), typeof(BindableTextBox), 
                              new PropertyMetadata(new PropertyChangedCallback(BindableTextBox.onBoundSelectionLenghtChanged))); 

    private static void onBoundSelectionLenghtChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     ((TextBox)d).SelectionLength = (int)e.NewValue; 
    } 

    public int BoundSelectionStart 
    { 
     get { return (int)GetValue(BoundSelectionStartProperty); } 
     set { SetValue(BoundSelectionStartProperty, value); } 
    } 

    public int BoundSelectionLenght 
    { 
     get { return (int)GetValue(BoundSelectionLenghtProperty); } 
     set { SetValue(BoundSelectionLenghtProperty, value); } 
    } 
} 

但是當我嘗試綁定的東西BoundSelectionStart它說,它說,我只能綁定到DP。

<bindable:BindableTextBox Text="{Binding Name}" BoundSelectionStart="{Binding ElementName=slider1, Path=Value}" /> 

什麼問題?

回答

2

你必須在該行一個錯字:

public static readonly DependencyProperty BoundSelectionStartProperty = DependencyProperty.Register(...) 

第一個參數應該是 「BoundSelectionStart」(2X E在選擇),而不是 「BoundSelctionStart」。

+0

您在多個地方也拼寫爲「長度」爲「長度」。 – user200783 2009-10-28 13:34:30

+0

謝謝,我應該給自己買一副眼鏡:) – kamilw 2009-10-28 13:42:27