Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-ObservableCollection绑定失败-.NET 4.0_C#_.net_Wpf_Observablecollection - Fatal编程技术网

C# WPF-ObservableCollection绑定失败-.NET 4.0

C# WPF-ObservableCollection绑定失败-.NET 4.0,c#,.net,wpf,observablecollection,C#,.net,Wpf,Observablecollection,快速总结 在我的应用程序的GUI中,我的网格中没有显示任何项目 当网格启动时,会在网格中列出正确的列(拾取集合类型) CollectionChanged事件在网格绑定到的ObservableCollection上激发 输出窗口中没有绑定错误(绑定成功但未注册委托) MyView.xaml . . . <UserControl.Resources> <xcdg:DataGridCollectionViewSource x:

快速总结

  • 在我的应用程序的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}}"
     .
     .
     .
MyViewModel.cs关键部件

 public MyTree Tree { get; set; }

 public MyViewModel()
 {    
         Tree = new MyTree();            
 }
 public ObservableCollection<Item> Items{ get; private set; }

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

 public MyTree Tree { get; set; }

 public MyViewModel()
 {    
         Tree = new MyTree();            
 }
 public ObservableCollection<Item> Items{ get; private set; }

 public MyTree()
 {
    Items= new ObservableCollection<Item>();
    Items.CollectionChanged += delegate { Console.WriteLine("Trigger"); };
 }
在这里,我认为问题在于
默认更新触发器被解析为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)
BindingExpression(hash=43320496):默认模式解析为单向
BindingExpression(hash=43320496):默认更新触发器已解析为PropertyChanged
BindingExpression(hash=43320496):附加到Xceed.Wpf.DataGrid.DataGridControl.ItemsSource(hash=9150720)
BindingExpression(哈希=43320496):解析源
BindingExpression(hash=43320496):找到的数据上下文元素:(OK)
BindingExpression(哈希=43320496):使用DataGridCollectionViewSource(哈希=9026257)中的视图
BindingExpression(hash=43320496):使用根项激活
BindingExpression(hash=43320496):使用访问器{DependencyProperty.UnsetValue}将级别0处的项替换为
BindingExpression(hash=43320496):使用以下命令获取级别0处的值:
BindingExpression(hash=43320496):TransferValue-获取原始值
BindingExpression(hash=43320496):TransferValue-使用最终值
BindingExpression(hash=43320496):从DataGridCollectionViewSource(hash=9026257)获取视图的PropertyChanged事件
BindingExpression(哈希=43320496):停用
BindingExpression(hash=43320496):将级别0处的项替换为{NullDataItem}
BindingExpression(哈希=43320496):使用DataGridCollectionViewSource(哈希=9026257)中的视图
BindingExpression(hash=43320496):使用根项DataGridCollectionView激活(hash=54545236 Count=0)
BindingExpression(hash=43320496):使用访问器{DependencyProperty.UnsetValue}将级别0处的项替换为DataGridCollectionView(hash=54545236 Count=0)
BindingExpression(hash=43320496):使用:DataGridCollectionView(hash=54545236 Count=0)从DataGridCollectionView(hash=54545236 Count=0)获取级别0处的值
BindingExpression(hash=43320496):TransferValue-获取原始值DataGridCollectionView(hash=54545236 Count=0)
BindingExpression(hash=43320496):TransferValue-使用最终值DataGridCollectionView(hash=54545236 Count=0)

如果尝试将网格直接绑定到
属性,而不是绑定到
集合视图源
?如果这样做有效,那么问题在于将
CollectionViewSource
绑定到您的集合,而不是网格绑定到
CollectionViewSource

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


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

您必须设置
CollectionViewSource
Source
属性:

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

您可以尝试为CollectionViewSource指定一个名称,然后在加载数据后在代码隐藏中对其调用CollectionViewSource.View.Refresh()方法

很多时候,问题是即使将视图设置为可观察集合,CollectionViewSource也不会自动更新

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


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

是否愿意对一些答案进行评论?当然,我遇到了另一个问题,我已经尝试了一半,但运气不佳,明天上午将尝试其余部分。我非常感谢你的帮助,我很惊讶得到这么多好的建议。