0

我正在尝试绑定到 CollectionViewSource 嵌套属性(CVS.View.Groups.Count),但它似乎在代码中不起作用:

Binding binding = new Binding();
binding.Path = new PropertyPath("View.Groups.Count");
binding.Mode = BindingMode.OneWay;
binding.Source = CVS;
BindingOperations.SetBinding(this, ValueProperty, binding);

但它在 WPF/xaml 中运行良好。

<DataTrigger Binding="{Binding Path=CVS.View.Groups.Count, Mode=OneWay}" Value="1">

所以我想知道这两种方法之间有什么区别以及代码方式绑定有什么问题。同时,当它是依赖对象中的简单依赖属性时,这种代码在非嵌套属性上运行良好,所以我认为提供的 PropertyPath 存在问题。

任何帮助,将不胜感激。

4

1 回答 1

0

我通过更改绑定源成功解决了问题。我没有直接传递依赖对象,而是使用依赖对象创建了一个 Windows.Forms.BindingSource,并将该对象设置为绑定源。通过这种方式,绑定现在运行良好。

Binding binding = new Binding();
binding.Path = new PropertyPath("View.Groups.Count");
binding.Mode = BindingMode.OneWay;

System.Windows.Forms.BindingSource bs = new System.Windows.Forms.BindingSource(DevicesInAlarmCVS, null);
binding.Source = bs;

BindingOperations.SetBinding(this, ValueProperty, binding);

它似乎与 .NET framework 4.0 中的变化有关: 数据绑定是否支持 Windows 窗体中的嵌套属性?

希望这能有所帮助

于 2016-01-01T16:20:03.677 回答