2016-08-08 8 views
0

デザイン時にツリービューにサンプルデータが表示されるようにしたいと考えています。 My Treeviewには、ネストされたTreeviewsとCollectionViewSourcesが含まれています。CollectionViewSourcesグループとHierarchicalDataTemplateを使用したデザイン時ツリービューのバインド

ネストされたツリービューを表示する方法を知りたいです(3つのレベルのノードのうち、最初のノードのみが現在表示されています)。

私のツリービューには、/状態(例えば "レディ")>日付(すなわち、「8月8日のオブジェクト(階層データが含まれています:ツリービュー

について

は、ここで私はこれまでに考え出したものです16" )>名前(すなわち "りんご")が、私は一つだけを取得することができ

ツリービューが支えられて:。

  • ObjectTreeviewViewModel(ObservableColle ObjectDateDisplay

現状でグループにStateDisplay(+ソーティング)

  • 別CollectionViewSourceによってグループにction)
  • CollectionViewSource

    ObjectTreeviewのみ、設計時にノードの1つのレベルを示します私はノードの3つのレベルを期待しています。

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

    マイスタック

    私はXAML

    のVisual Studio 2015

    コード

    を使用して、.NET 4.5でWPFアプリケーションを構築しています

    <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つのノードの中間に制御される1つ)でした。外側の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:Source=ObjectTreeviewDesignTimeData.xamlとDesignSource(私たちは上記使用するのと同じもの)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と同じコレクションを使用して動作させる方法を見つけることです。

    関連する問題