2010-11-26 83 views
0

快速汇总WPF - 的ObservableCollection绑定失败 - .NET 4.0

  • 没有项目在我在我的应用
  • 正确列在格列时,它会调用的GUI网格显示(拿起集合型)
  • 在网格上被绑定到
  • 在输出窗口中没有结合错误的ObservableCollection CollectionChanged事件触发(绑定成功但未注册代表)

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键Pa RTS

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) 
+0

注意评论一些答案? – VVS 2010-11-29 21:21:01

回答

0

如果直接绑定到您的项目而不是使用静态源,它会工作吗?

<xcdg:DataGridControl Name="dataGrid" ItemsSource="{Binding Path=Items}" />

我没有与静态项目源多的经验,但静态的字让我觉得它是一个一次性的结合

0

如果你尝试直接绑定网格到Items财产,而不是向CollectionViewSource?如果有效,那么问题在于将CollectionViewSource绑定到您的收藏集,而不是网格绑定到CollectionViewSource

0

您必须设置CollectionViewSourceSource属性:

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

你可以尝试的东西是给CollectionViewSource一个名字,然后在代码后面调用它的CollectionViewSource.View.Refresh()方法,在它之后加载数据。

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

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

试试这个,让我知道它是否解决了你的问题。我也有AutoRefreshingObservableCollection的代码,如果你需要,回复,我可以把它放在我的网站上或其他东西。