UserControl 绑定仅在构造函数中工作

UserControl Binding only working in constructor

我试图在我的代码中将 UserControl 绑定到 属性。 绑定在构造函数中工作,但是当我尝试在按下按钮后为 属性 分配另一个值时,UserControl 不会更改它的值。

XAML:

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

绑定属性:

public UserControl MainDock { get; set; }

构造函数:

public DBControl()
{
  InitializeComponent();

  this.DataContext = this;

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

按钮方法:

private void ShowItemsToPrint(object sender, RoutedEventArgs e)
{
  ItemsToPrintControl sitp = new ItemsToPrintControl();
  MainDock = sitp;
}

您需要致电 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));
    }
}