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(...)

最初のパラメーターは、「BoundSelctionStart」ではなく、「BoundSelectionStart」(Selectionでは2x e)である必要があります。

于 2009-10-28T13:34:09.310 に答える