2009-10-28 104 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