UserControl Binding only working in constructor

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;
}

1 answer

  • answered 2021-05-04 13:57 Wellerman

    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));
        }
    }