Как использовать привязку TwoWay из UserControl?

У меня есть собственный пользовательский элемент управления, LabeledTextBox, который представляет собой комбинацию Labelи.. хорошо, TextBox. Этот элемент управления имеет два свойства :Caption, которые будут привязаны к заголовку Labelи Value, которые будут связаны с Textиз TextBox.

Код:

public class LabeledTextBox : Control
{
    static LabeledTextBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(LabeledTextBox), new FrameworkPropertyMetadata(typeof(LabeledTextBox)));
    }

    public string Caption
    {
        get { return (string)GetValue(CaptionProperty); }
        set { SetValue(CaptionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Caption.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CaptionProperty =
        DependencyProperty.Register("Caption", typeof(string), typeof(LabeledTextBox), new UIPropertyMetadata(""));


    public string Value
    {
        get { return (string)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(string), typeof(LabeledTextBox), new UIPropertyMetadata(""));


}

XAML:


Использование:


Сначала я думал, что нашел ответ здесь:WPF TemplateBinding vs RelativeSource TemplatedParent

В этом подробно описывается разница между TemplateBindingи RelativeSource TemplatedParent. Я соответственно изменил свой код, но мне все еще кажется, что я пропустил шаг. Привязка OneWay работает, мое текстовое поле привязано к свойству Value, но изменения не регистрируются.

Как заставить это работать?

6
задан Community 23 May 2017 в 12:10
поделиться