c# - Design-time treeview binding with CollectionViewSources groups and HierarchicalDataTemplate -


i'd sample data appear in treeview during design time. treeview contains nested treeviews , collectionviewsources.

i'd find out how nested treeviews show (only first of 3 levels of nodes show currently).

about treeview

here's i've figured out far:

my treeview contains hierarchical data (object states (e.g. "ready") > date (i.e. "8/8/16") > name (i.e. "apples"), can one.

the treeview backed by:

  • objecttreeviewviewmodel (a observablecollection)
  • collectionviewsource group statedisplay (+ sorting)
  • another collectionviewsource group objectdatedisplay

current status

the objecttreeview shows 1 level of nodes @ design time, when i'm expecting 3 levels of nodes.

treeview (wpf) shows 1 level of nodes in design time- should show 3

my stack

i'm building wpf application on .net 4.5, using visual studio 2015.

code

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 resources

    <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 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 object dates (this needs nested in treeview.resources) -->                                     <collectionviewsource x:key="objectdatecollectionviewsource"                                                                                 source="{binding path=items}"                                                                                 d:designsource="{binding path=items}"                                                                                 >                                               <collectionviewsource.groupdescriptions>                                                     <propertygroupdescription propertyname="objectdatedisplay"/>                                             </collectionviewsource.groupdescriptions>                                      </collectionviewsource>                                      <!-- [this , children] hide 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> 

code-behind

    using system.windows.controls;     using myproject.app.viewmodels.mainwindow;     using myproject.lib.enumerations;      namespace myproject.app.views.mainwindow     {             /// <summary>             /// interaction logic objecttreeview.xaml             /// </summary>             public partial class objecttreeview : usercontrol             {                     public objecttreeviewviewmodel objecttreeviewviewmodel { get; private set; } = new objecttreeviewviewmodel(); // observablecollection<objectviewmodel>                      public objecttreeview()                     {                             initializecomponent();                     }                      /// <summary>                     ///     load object objectstategroup (a set of objectstates) collection backs treeview.                     /// </summary>                     /// <param name="objectstategroup">the objectstategroupsenum load.</param>                     public void loadobjectstategroup(objectstategroupsenum objectstategroup)                     {                             objecttreeviewviewmodel.loadobjectstategroup(objectstategroup);                     }             }     } 

i found workaround problem.

the issue

the issue inner collectionviewsource (the 1 controlled middle of 3 nodes). outer collectionviewsource showing node.

the design time binding couldn't use same path=items run time using.

<collectionviewsource x:key="objectdatecollectionviewsource"                       source="{binding path=items}"                       d:designsource="{binding path=items}">                   <collectionviewsource.groupdescriptions>                                 <propertygroupdescription propertyname="objectdatedisplay"/>                 </collectionviewsource.groupdescriptions>  </collectionviewsource> 

the solution

update d:designsource load xaml example file (the same 1 use above) 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> 

after setting d:designsource , re-building, 3 levels of nodes started appearing @ design time. confirmed issue design-time binding inner collectionviewsource.

a sufficient workaround

by binding same dictionary in outer , inner collectionviewsources, redundant data generated. (the dictionary loaded multiple times, each sub-treeview.) however, ok, i'm in designer mode , need placeholder data.

a better solution find way inner cvs d:designersource="{better_solution_here}" part work using same collection outer cvs.


Comments