2010-11-26 107 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的代碼,如果你需要,回覆,我可以把它放在我的網站上或其他東西。