【问题标题】:Data binding failing when trying to bind from MainWindow code-behind to UserControl code-behind尝试从 MainWindow 代码隐藏绑定到 UserControl 代码隐藏时数据绑定失败
【发布时间】:2015-12-30 20:32:50
【问题描述】:

我的 MainWindow 使用 INotifyPropertyChanged 接口。我正在使用我已经使用了一段时间的 OnPropertyChanged 函数,该函数有效。
在我的 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;
}

我的 MainWindow xaml 所做的就是:

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

我的 UserControl 有两个依赖属性:UcOcObservableCollection&lt;bool&gt;UcBoolbool
这是我的用户控件代码:

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

我的 UserControl xaml 是空的(只有默认的 &lt;Grid&gt;&lt;/Grid&gt;

这个程序的输出是

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

当 DataContext 发生变化时,我应该如何更新 UserControl 属性?

【问题讨论】:

  • 编辑代码并添加两个依赖属性以获得更好的帮助...
  • DataContextChanged 被触发后,绑定的属性可能尚未更新。您应该改为处理UserControl 的事件TargetUpdated。但是不确定为什么要获取 DataContextChanged 事件处理程序中的值。
  • 我只检查了DataContextChanged 事件处理程序,因为我认为这些值会在那时更新。我在UserControl 中添加了TargetUpdated 事件处理程序,但它从未被触发。
  • 看来您需要将 Binding 的 NotifyOnTargetUpdated 设置为 true。例如:你需要UcOc="{Binding MwOc, NotifyOnTargetUpdated=true}"而不是UcOc="{Binding MwOc}"
  • 是的。我以这种方式测试 UcOc="{Binding MwOc, NotifyOnTargetUpdated=true}" 触发 TargetUpdated 事件。

标签: c# wpf xaml data-binding user-controls


【解决方案1】:

在 MainWindow xaml 中,绑定需要将 NotifyOnTargetUpdated 属性设置为 true。
而不是:

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

使用:

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

在 UserControl 中,订阅DataContextChanged 事件也会导致ObservableCollection&lt;bool&gt; 上的绑定失败,但bool 上的绑定不会失败。由于目前未知的原因。

【讨论】:

    猜你喜欢
    • 2013-10-17
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    相关资源
    最近更新 更多