2016-08-08 41 views
0

我想在設計時獲取示例數據以顯示在我的Treeview中。我的Treeview包含嵌套的Treeviews和CollectionViewSource。設計時樹視圖與CollectionViewSources組和HierarchicalDataTemplate的綁定

我想知道如何讓嵌套的TreeView顯示(只顯示當前3個節點中的第一個顯示)。

關於樹形

這是我到目前爲止已經想通了:

我的樹視圖包含分層數據(對象國(如 「就緒」)>日期(即「8/8/。

  • ObjectTreeviewViewModel(一ObservableColle:16" )>名稱(即 「蘋果」),但我只能得到一個

    TreeView所後盾ction)

  • CollectionViewSource到組由StateDisplay(+分選)
  • 另一個CollectionViewSource到組由ObjectDateDisplay

現狀

的ObjectTreeview僅示出了在設計時節點中的一個電平的時候,我期待3級節點。

Treeview (WPF) only shows 1 level of nodes in design time- should show 3

我的堆疊

我在.NET 4.5構建一個WPF應用程序,使用Visual Studio 2015年

代碼

XAML

<UserControl x:Class="myproject.app.views.MainWindow.ObjectTreeview" 
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          mc:Ignorable="d" 
          d:DesignHeight="300" d:DesignWidth="300" 
          DataContext="{Binding RelativeSource={RelativeSource Self}}" 
          > 

      <UserControl.Resources> 

        <ResourceDictionary> 
          <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="../../resources/MainWindow/ObjectTreeviewResources.xaml"></ResourceDictionary> 
          </ResourceDictionary.MergedDictionaries> 
        </ResourceDictionary> 

      </UserControl.Resources> 

      <TreeView x:Name="Treeview" 
             ItemsSource="{Binding Source={StaticResource ObjectStateCollectionViewSource}, Path=Groups}" 
             ItemTemplate="{Binding Source={StaticResource ObjectStateTemplate}}"> 
      </TreeView> 

    </UserControl> 

XAML資源

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
              xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
              xmlns:pf="clr-namespace:System.ComponentModel;assembly=PresentationFramework" 
              xmlns:mainWindow="clr-namespace:myproject.app.viewmodels.MainWindow" 
              mc:Ignorable="d"> 

      <!-- I.e. Level-3 Node (i.e. Leaf nodes) --> 
      <DataTemplate x:Key="ObjectTreeviewNode"> 
        <TextBlock Text="{Binding ObjectNameDisplay}"/> 
      </DataTemplate> 


      <!-- Initial Grouping: Group by object states --> 
      <CollectionViewSource x:Key="ObjectStateCollectionViewSource" 
                 Source="{Binding Path=ObjectTreeviewViewModel.TreeviewCollection}" 
                 d:DesignSource="{d:DesignData Source=ObjectTreeviewDesignTimeData.xaml}" 
                 > 
        <CollectionViewSource.GroupDescriptions> 
          <PropertyGroupDescription PropertyName="StateDisplay"/> 
        </CollectionViewSource.GroupDescriptions> 
        <CollectionViewSource.SortDescriptions> 
          <componentModel:SortDescription PropertyName="StateEnum" /> 
          <componentModel:SortDescription PropertyName="ObjectDate" /> 
          <componentModel:SortDescription PropertyName="ObjectNameDisplay" /> 
        </CollectionViewSource.SortDescriptions> 
      </CollectionViewSource> 

      <!-- I.e. Level-2 Node (i.e. mid-nodes) --> 
      <HierarchicalDataTemplate x:Key="ObjectDateTemplate"> 
        <TreeView BorderThickness="0"> 
          <TreeViewItem Header="{Binding Path=Name}" 
                 ItemsSource="{Binding Path=Items}" 
                 d:DataContext="{Binding Path=Items}" 
                 ItemTemplate="{StaticResource ResourceKey=ObjectTreeviewNode}" 
                 IsExpanded="True"/> 
        </TreeView> 
      </HierarchicalDataTemplate> 

      <!-- I.e. Level-1 Node (i.e. Root nodes) --> 
      <HierarchicalDataTemplate x:Key="ObjectStateTemplate" > 
        <TreeView BorderThickness="0"> 

          <TreeView.Resources> 

            <!-- Sub-grouping: Group by object dates (This needs to be nested in this Treeview.Resources) --> 
            <CollectionViewSource x:Key="ObjectDateCollectionViewSource" 
                       Source="{Binding Path=Items}" 
                       d:DesignSource="{Binding Path=Items}" 
                       > 

              <CollectionViewSource.GroupDescriptions> 
                <PropertyGroupDescription PropertyName="ObjectDateDisplay"/> 
              </CollectionViewSource.GroupDescriptions> 

            </CollectionViewSource> 

            <!-- [This and all children] Hide the light-grey inactive background --> 
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" /> 

          </TreeView.Resources> 

          <TreeViewItem Header="{Binding Path=Name}" 
                 ItemsSource="{Binding Source={StaticResource ObjectDateCollectionViewSource}, Path=Groups}" 
                 ItemTemplate="{StaticResource ObjectDateTemplate}" 
                 IsExpanded="True"/> 

        </TreeView> 
      </HierarchicalDataTemplate> 
    </ResourceDictionary> 

代碼隱藏

using System.Windows.Controls; 
    using myproject.app.viewmodels.MainWindow; 
    using myproject.lib.enumerations; 

    namespace myproject.app.views.MainWindow 
    { 
      /// <summary> 
      /// Interaction logic for ObjectTreeview.xaml 
      /// </summary> 
      public partial class ObjectTreeview : UserControl 
      { 
        public ObjectTreeviewViewModel ObjectTreeviewViewModel { get; private set; } = new ObjectTreeviewViewModel(); // this is a ObservableCollection<ObjectViewModel> 

        public ObjectTreeview() 
        { 
          InitializeComponent(); 
        } 

        /// <summary> 
        ///  Load object for an objectStateGroup (a set of ObjectStates) into the collection that backs the treeview. 
        /// </summary> 
        /// <param name="objectStateGroup">The objectStateGroupsEnum to load.</param> 
        public void LoadObjectStateGroup(objectStateGroupsEnum objectStateGroup) 
        { 
          ObjectTreeviewViewModel.LoadobjectStateGroup(objectStateGroup); 
        } 
      } 
    } 

回答

0

我找到了一個解決辦法,以我的問題。

的問題

的問題是與內CollectionViewSource(即控制3個節點的中間的那一個)。只有外部的CollectionViewSource顯示其節點。

設計時綁定不能使用運行時使用的相同Path=Items

<CollectionViewSource x:Key="ObjectDateCollectionViewSource" 
         Source="{Binding Path=Items}" 
         d:DesignSource="{Binding Path=Items}"> 

       <CollectionViewSource.GroupDescriptions> 
           <PropertyGroupDescription PropertyName="ObjectDateDisplay"/> 
       </CollectionViewSource.GroupDescriptions> 

</CollectionViewSource> 

更新d:DesignSource從XAML示例文件(同一個我們上面使用)加載具有Source=ObjectTreeviewDesignTimeData.xaml

<CollectionViewSource x:Key="ObjectDateCollectionViewSource" 
          Source="{Binding Path=Items}" 
          d:DesignSource="{d:DesignData Source=ObjectTreeviewDesignTimeData.xaml}"> 

      <CollectionViewSource.GroupDescriptions> 
        <PropertyGroupDescription PropertyName="ObjectDateDisplay"/> 
      </CollectionViewSource.GroupDescriptions> 

    </CollectionViewSource> 

在設置d:DesignSource並重新構建之後,3個級別的節點開始出現在設計時。我確認設計時綁定的問題與內部CollectionViewSource有關。

一個充分解決方法

通過結合在外側和內側CollectionViewSources相同的字典,生成冗餘數據。 (對於每個子樹視圖,字典會多次加載。)但是,這是好的,因爲我處於設計器模式,我只需要佔位符數據。

更好的解決方案是找到一種方法讓內部CVS d:DesignerSource="{BETTER_SOLUTION_HERE}"部分使用與外部CVS相同的集合工作。