У меня есть собственный пользовательский элемент управления, 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, но изменения не регистрируются.
Как заставить это работать?