3

我試圖顯示一個Wpf樹視圖與按CollectionViewSource排序的項目。將Wpf HierarchicalDataTemplate ItemsSource綁定到字典中的CollectionViewSource?

目前,一切工作除了在我的資源字典使用此代碼排序:

<HierarchicalDataTemplate DataType="{x:Type books:Container}" ItemsSource="{Binding Path=Items}"> 
    <nav:ContainerControl /> 
</HierarchicalDataTemplate> 

什麼將成爲改變HierarchicalDataTemplate綁定到一個CollectionViewSource,反過來從項目屬性拉語法?

我試過the code posted on Bea Stollnitz's blog的變體,但沒有成功。我無法弄清楚如何設置CollectionViewSource的來源。

+0

偉大的問題;我自己也想問這個問題。謝謝! :-) – 2011-08-28 01:07:40

回答

5

那麼讓我說我討厭我提出的解決方案,但它確實有效。也許WPF大師會用更好的選擇來啓發我們。當然,如果您在視圖背後使用ViewModel,則可以在ViewModel中用CollectionView簡單地包裝模型的Items屬性,並完成它。

但這是另一種解決方案。基本上,您的HierarchicalDataTemplate可以保持原樣,除了將Converter添加到Binding。我實現了以下轉換器並相應地更改了XAML。

<HierarchicalDataTemplate DataType="{x:Type books:Container}" 
    ItemsSource="{Binding Items, Converter={x:Static local:CollectionViewConverter.Instance}}"> 
    <nav:ContainerControl /> 
</HierarchicalDataTemplate> 

CollectionViewConverter.cs

public class CollectionViewConverter : IValueConverter 
{ 

    public CollectionViewConverter() {} 

    static CollectionViewConverter(){ 
     Instance = new CollectionViewConverter(); 
    } 

    public static CollectionViewConverter Instance { 
     get; 
     set; 
    } 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var view = new ListCollectionView((System.Collections.IList)value); 
     view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); 
     return view; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     // not really necessary could just throw notsupportedexception 
     var view = (CollectionView)value; 
     return view.SourceCollection; 
    } 
} 
+0

感謝您的解決方案。 – awx 2010-01-20 06:26:40

+0

但是,此解決方案導致一個小問題。該警告重複打印在Visual Studio輸出窗口中。 「System.Windows.Data Error:4:Can not find source with binding with reference'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1''。BindingExpression:Path = VerticalContentAlignment; DataItem = null;目標元素是'TreeViewItem'(Name ='');目標屬性是'VerticalContentAlignment'(類型'VerticalAlignment')「 MSDN組中的MS人員表示可以安全地忽略它。 – awx 2010-01-20 06:33:53

+0

該錯誤聽起來像來自TreeViewItem的默認模板,而不是您的問題處理的綁定。 – Josh 2010-01-20 06:44:55

0

我確實如你所說包裹項目集合用的ListCollectionView:

private SortDescription _ItemsLcvSortDesc; 
    private SortDescription ItemsLcvSortDesc 
    { 
     get 
     { 
      if (_ItemsLcvSortDesc == null) 
       _ItemsLcvSortDesc = new SortDescription("SortOrder", ListSortDirection.Ascending); 
      return _ItemsLcvSortDesc; 
     } 
    } 

    private ListCollectionView _ItemsLcv; 
    public ListCollectionView ItemsLcv 
    { 
     get 
     { 
      if (_ItemsLcv == null) 
       _ItemsLcv = CollectionViewSource.GetDefaultView(Items) as ListCollectionView; 
      _ItemsLcv.SortDescriptions.Add(ItemsLcvSortDesc); 
      return _ItemsLcv; 
     } 
    } 

我錯過了什麼?

相關問題