【问题标题】:Binding on CollectionViewSource -> View -> Groups -> Count (Code vs XAML)绑定 CollectionViewSource -> 视图 -> 组 -> 计数(代码与 XAML)
【发布时间】:2016-04-05 20:57:49
【问题描述】:

我正在尝试绑定到 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 存在问题..

任何帮助将不胜感激。

【问题讨论】:

    标签: binding dependency-properties dependencyobject


    【解决方案1】:

    我通过更改绑定源成功解决了问题。我没有直接传递依赖对象,而是使用依赖对象创建了一个 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 的变化有关: Does data binding support nested properties in Windows Forms?

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2011-03-01
      • 2015-12-14
      • 2017-07-27
      • 1970-01-01
      • 2017-02-22
      相关资源
      最近更新 更多