简体   繁体   English

尝试从MainWindow后台代码绑定到UserControl后台代码时,数据绑定失败

[英]Data binding failing when trying to bind from MainWindow code-behind to UserControl code-behind

My MainWindow uses the INotifyPropertyChanged interface. 我的MainWindow使用INotifyPropertyChanged接口。 I'm using the OnPropertyChanged function that I've been using for a while, which works. 我正在使用一段时间的OnPropertyChanged函数,该函数有效。
In my MainWindow code-behind I have this: 在我的MainWindow代码背后,我有这个:

public ObservableCollection<bool> MwOc { get; set; }

private bool _mwBool;
public bool MwBool { get { return _mwBool; } set { _mwBool = value; OnPropertyChanged(); } }

public MainWindow()
{
    InitializeComponent();

    MwOc = new ObservableCollection<bool>();

    MwOc.Add(false);
    MwBool = true;

    Console.WriteLine("MwOc:   " + MwOc.Count);
    Console.WriteLine("MwBool: " + MwBool);

    DataContext = this;
}

All my MainWindow xaml does is this: 我MainWindow xaml所做的全部是这样的:

<local:UserControl1 x:Name="Control" UcOc="{Binding MwOc}" UcBool="{Binding MwBool}" />

My UserControl has two dependency properties: UcOc an ObservableCollection<bool> and UcBool a bool 我的UserControl有两个依赖项属性: UcOc一个ObservableCollection<bool>UcBool一个bool
Here is my UserControl code: 这是我的UserControl代码:

public ObservableCollection<bool> UcOc
{
    get { return (ObservableCollection<bool>)GetValue(UcOcProperty); }
    set { SetValue(UcOcProperty, value); }
}

public static readonly DependencyProperty UcOcProperty =
DependencyProperty.Register("UcOc", typeof(ObservableCollection<bool>), typeof(UserControl1));

public bool UcBool
{
    get { return (bool)GetValue(UcBoolProperty); }
    set { SetValue(UcBoolProperty, value); }
}

public static readonly DependencyProperty UcBoolProperty =
    DependencyProperty.Register("UcBool", typeof(bool), typeof(UserControl1));

public UserControl1()
{
    InitializeComponent();
    UcOc = UcOc ?? new ObservableCollection<bool>();

    DataContextChanged += (o, e) => { Console.WriteLine("DataContextChanged"); Print(); };
}

public void Print()
{
    UcOc = UcOc ?? new ObservableCollection<bool>();
    Console.WriteLine("UcOc:   " + UcOc.Count);
    Console.WriteLine("UcBool: " + UcBool);
}

My UserControl xaml is empty (just has the default <Grid></Grid> ) 我的UserControl xaml为空(只有默认的<Grid></Grid>

The output of this program is 该程序的输出是

MwOc:   1
MwBool: True
DataContextChanged
UcOc:   0
UcBool: False

How should I update the UserControl properties when its DataContext changes? 当DataContext更改时,应如何更新UserControl属性?

In the MainWindow xaml, the bindings need the NotifyOnTargetUpdated property set to true. 在MainWindow xaml中,绑定需要将NotifyOnTargetUpdated属性设置为true。
Instead of: 代替:

<local:UserControl1 x:Name="Control" UcOc="{Binding MwOc}" UcBool="{Binding MwBool}" />

use: 采用:

<local:UserControl1 x:Name="Control" UcOc="{Binding MwOc, NotifyOnTargetUpdated=true}" UcBool="{Binding MwBool, NotifyOnTargetUpdated=true}" />

In the UserControl, subscribing to the DataContextChanged event also causes the binding to fail on the ObservableCollection<bool> but not the bool . 在UserControl中,订阅DataContextChanged事件也会导致绑定在ObservableCollection<bool>上失败,但在bool上失败。 For currently unknown reasons. 由于目前未知的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM