0

I cannot properly bind to a UserControl property placed in a Page.

I have this UserControl :

<UserControl x:Class="xxxx.NumericBox" (...)>
    <TextBox Name="TextBoxValue" Text="{Binding RelativeSource {RelativeSource AncestorType=UserControl}, Path=Value, Mode=TwoWay}" (...)

With this behind code :

  public partial class NumericBox : UserControl
  {

    public NumericBox()
    {
      InitializeComponent();
    }

    public uint? Value
    {
      get => (uint?)GetValue(ValueProperty);
      set => SetValue(ValueProperty, value);
    }

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(uint?), typeof(NumericBox), new PropertyMetadata(null));

The UserControl contains others controls witch interract with Value property (+/-) and it works fine.

But I create the DependencyProperty to also bind the value in parent page.

A exemple of code in a page where I inject the UserControl :

var binding = new Binding("Line.Quantity");
binding.Mode = BindingMode.TwoWay;
var numeric = new NumericBox();
numeric.SetBinding(ValueProperty, binding);

The binding works on startup but not update Line.Quantity when I modify the Textbox... The Line class implements INotifyPropertyChanged and notify change on Quantity.

What is the correct way to do that ?

I have seen this question but but I have not been able to correct my code : Binding on DependencyProperty of custom User Control not updating on change

A.Baudouin
  • 2,855
  • 3
  • 24
  • 28

0 Answers0