0

我试图克服一个不允许我绑定到常规 clr 属性的限制。

我使用的解决方案使用自定义依赖属性,这些属性反过来会更改 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}" />

问题是什么?

4

1 回答 1

2

您在该行中有一个错字:

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

第一个参数应该是“BoundSelectionStart”(Selection 中的 2x e),而不是“BoundSelctionStart”。

于 2009-10-28T13:34:09.310 回答