【问题标题】:WPF - ObservableCollection Binding Failure - .NET 4.0WPF - ObservableCollection 绑定失败 - .NET 4.0
【发布时间】:2010-11-27 09:25:27
【问题描述】:

快速总结

  • 在我的应用程序 GUI 的网格中没有显示任何项目
  • 启动时网格中会列出正确的列(拾取集合类型)
  • CollectionChanged 事件在网格绑定到的 ObservableCollection 上触发
  • 输出窗口无绑定错误(绑定成功但未注册委托)

MyView.xaml

 .
 .
 .
<UserControl.Resources>
        <xcdg:DataGridCollectionViewSource
                x:Key="items"
                Source="{Binding Path=Items}"
                AutoFilterMode="And" >
        </xcdg:DataGridCollectionViewSource>
</UserControl.Resources>
<xcdg:DataGridControl 
        Name="dataGrid" 
        ReadOnly="True"
        ItemsSource="{Binding Source={StaticResource items}}"
     .
     .
     .

MyView.xaml.cs 构造函数

    public MyView()
    {
        if (!DesignerProperties.GetIsInDesignMode(this))
            DataContext = new MyViewModel().Tree;
        initializeComponent();
    }

MyViewModel.cs 关键部分

 public MyTree Tree { get; set; }

 public MyViewModel()
 {    
         Tree = new MyTree();            
 }

MyTree.cs 关键部分

 public ObservableCollection<Item> Items{ get; private set; }

 public MyTree()
 {
    Items= new ObservableCollection<Item>();
    Items.CollectionChanged += delegate { Console.WriteLine("Trigger"); };
 }

每次添加和删除时都会打印触发器,但 UI 仍然认为集合是空的并且不知道更新。没有我的额外委托 Items.CollectionChangednull(即解析的 Xaml 不会导致将侦听器添加到集合中)

  • 我做错了什么尝试 绑定我的 DataGridCollectionViewSource 给一个 ObservableCollection?

很高兴提供更多细节,我已尝试从我的用例抽象到问题的核心。集合的嵌套可能看起来很荒谬,但它基本上是树中节点的集合,以便更快地访问。

提前致谢!

编辑

通过将PresentationTraceSources.TraceLevel=High 添加到我的 Xaml 中,我得到了一些更详细的输出。

 ItemsSource="{Binding Source={StaticResource orders}, PresentationTraceSources.TraceLevel=High}"

我认为问题在于Default update trigger resolved to PropertyChanged 我希望默认为CollectionChanged。不过,我仍然不确定该怎么做。

BindingExpression (hash=43320496): Default mode resolved to OneWay
BindingExpression (hash=43320496): Default update trigger resolved to PropertyChanged
BindingExpression (hash=43320496): Attach to Xceed.Wpf.DataGrid.DataGridControl.ItemsSource (hash=9150720)
BindingExpression (hash=43320496): Resolving source 
BindingExpression (hash=43320496): Found data context element: <null> (OK)
BindingExpression (hash=43320496): Use View from DataGridCollectionViewSource (hash=9026257)
BindingExpression (hash=43320496): Activate with root item <null>
 BindingExpression (hash=43320496): Replace item at level 0 with <null>, using accessor {DependencyProperty.UnsetValue}
BindingExpression (hash=43320496): GetValue at level 0 from <null> using <null>: <null>
BindingExpression (hash=43320496): TransferValue - got raw value <null>
BindingExpression (hash=43320496): TransferValue - using final value <null>
BindingExpression (hash=43320496): Got PropertyChanged event from DataGridCollectionViewSource (hash=9026257) for View
BindingExpression (hash=43320496): Deactivate
BindingExpression (hash=43320496): Replace item at level 0 with {NullDataItem}
BindingExpression (hash=43320496): Use View from DataGridCollectionViewSource (hash=9026257)
BindingExpression (hash=43320496): Activate with root item DataGridCollectionView (hash=54545236 Count=0)
BindingExpression (hash=43320496): Replace item at level 0 with DataGridCollectionView (hash=54545236 Count=0), using accessor {DependencyProperty.UnsetValue}
BindingExpression (hash=43320496): GetValue at level 0 from DataGridCollectionView (hash=54545236 Count=0) using <null>: DataGridCollectionView (hash=54545236 Count=0)
BindingExpression (hash=43320496): TransferValue - got raw value DataGridCollectionView (hash=54545236 Count=0)
BindingExpression (hash=43320496): TransferValue - using final value DataGridCollectionView (hash=54545236 Count=0)

【问题讨论】:

  • 想评论一些答案吗?
  • 当然,我陷入了另一个问题,我尝试了一半没有运气,明天早上再尝试其余的。我非常感谢您的所有帮助,我很惊讶能得到这么多好的建议。

标签: c# .net wpf observablecollection


【解决方案1】:

如果您直接绑定到您的项目而不是使用静态源,它是否有效?

&lt;xcdg:DataGridControl Name="dataGrid" ItemsSource="{Binding Path=Items}" /&gt;

我对静态项目来源没有太多经验,但是静态这个词让我觉得它是一次性绑定

【讨论】:

    【解决方案2】:

    如果您尝试将 Grid 直接绑定到 Items 属性,而不是绑定到 CollectionViewSource,会怎样?如果这样可行,那么问题在于将 CollectionViewSource 绑定到您的集合,而不是 Grid 绑定到 CollectionViewSource

    【讨论】:

      【解决方案3】:

      你必须设置CollectionViewSourceSource属性:

          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
             var items = (CollectionViewSource) FindResource("items");
             items.Source = new MyViewModel().Tree;
          }
      

      【讨论】:

        【解决方案4】:

        您可以尝试为 CollectionViewSource 命名,然后在加载数据后在代码中调用 CollectionViewSource.View.Refresh() 方法。

        很多时候问题是即使您将视图设为 ObservableCollection,CollectionViewSource 也不会自动更新。

        网上有人发布了一个很好的 AutoRefreshingObservableCollection 类,我一直在使用它并且效果很好。

        试一试,如果它解决了您的问题,请告诉我。如果您需要,我也有 AutoRefreshingObservableCollection 的代码,请回复,我可以将其放在我的网站或其他地方。

        【讨论】:

          猜你喜欢
          • 2012-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-26
          • 2013-10-14
          • 2015-04-02
          • 2013-04-18
          • 1970-01-01
          相关资源
          最近更新 更多