0

I'm trying to bind a UserControl to a property in my code. The binding works in the constructor, but as I try to assign another value to the property after pressing a button, the UserControl does not change it's value.

XAML:

<UserControl Content="{Binding MainDock, UpdateSourceTrigger=PropertyChanged}"/>  

The binded property:

public UserControl MainDock { get; set; }

Constructor:

public DBControl()
{
  InitializeComponent();

  this.DataContext = this;

  MarkerControl mc = new MarkerControl();
  MainDock = mc;
}

Button method:

private void ShowItemsToPrint(object sender, RoutedEventArgs e)
{
  ItemsToPrintControl sitp = new ItemsToPrintControl();
  MainDock = sitp;
}
Oiproks
  • 764
  • 1
  • 9
  • 34
  • 2
    MainDock doesn't raise the PropertyChanged event nor does anything implement INotifyPropertyChanged from the code you have provided. – Ryan Thomas May 04 '21 at 13:52
  • As a note, setting `UpdateSourceTrigger=PropertyChanged` is pointless. It only has an effect in TwoWay or OneWayToSource Bindings. – Clemens May 04 '21 at 14:02

1 Answers1

0

You need to be calling OnPropertyChanged():

public class MyObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private UserControl _MainDock;
    public UserControl MainDock
    {
        get { return _MainDock; }
        set
        {
            _MainDock = value;
            OnPropertyChanged(nameof(MainDock));
        }
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
Wellerman
  • 846
  • 4
  • 14