views:

339

answers:

1

My co-worker threatened to put me on TheDailyWTF today because of my property I wrote to be used to build a 3-tiered treeview with ItemsControl.

I bear you the footprint:

ObservableCollection<KeyValuePair<string, ObservableCollection<KeyValuePair<string, ObservableCollection<MyType>>>>>;

My goal was to create an ItemsControl that would use the Key as the header, and Value as the ItemsSource for 3 levels:

<Style x:Key="filterTreeStyle" TargetType="ItemsControl">
        <Setter Property="ItemTemplate">
      <Setter.Value>
       <DataTemplate>
        <controls:TreeViewItem IsExpanded="True">
         <controls:TreeViewItem.Header>
          <controlsToolkit:TreeViewItemCheckBox Content="{Binding Key}"/>
         </controls:TreeViewItem.Header>
         <ItemsControl ItemsSource="{Binding Value}">
          <ItemsControl.ItemTemplate>
           <DataTemplate>
            <controls:TreeViewItem>
            <controls:TreeViewItem.Header>
             <controlsToolkit:TreeViewItemCheckBox Content="{Binding Key}"/>
            </controls:TreeViewItem.Header>
             <controlsToolkit:TreeViewItemCheckBox IsChecked="{Binding Enabled}" Content="{Binding FilterTypeText}"/>
            </controls:TreeViewItem>
           </DataTemplate>
          </ItemsControl.ItemTemplate>
         </ItemsControl>
        </controls:TreeViewItem>
       </DataTemplate>
      </Setter.Value>
     </Setter>
       </Style>

Can anyone save me from the clutches of TheDailyWTF? What is a cleaner way to do this. Bonus if we can figure out a way to make the number of levels dynamic.

+3  A: 

Uh, maybe I'm being dumb here, but since you want a TreeView... why not use a TreeView? You'll also need to use a HierarchicalDataTemplate instead of a vanilla DataTemplate: the content of the HDT becomes the Header, and the ItemsSource is used to create the child nodes. That will also take care of making the number of levels dynamic.

TreeView is built into WPF and is available in Silverlight as part of the SDK.

itowlson
Didn't know about HDT... looks like I can use that. Doesn't really change anything on the ItemsSource side (meaning I don't have a recursive method for depth).
eskerber
Not sure whether you mean the ItemsSource property side or the data model side? If the former, use the Value of the KeyValuePair, just like you're using at the moment. Thus, <HierarchicalDataTemplate ItemsSource="{Binding Value}">. If the latter, true, but you're better off creating a hierarchical data structure anyway, rather than using that deeply nested KVP arrangement -- this will give you more meaningful names than Key and Value as well! E.g. `class TreeNode { string Title; ObservableCollection<TreeNode> Children; }`
itowlson
I used the HDT and it works great. Bit of a pain because the Silverlight flavor requires every 'level' to have identical property names, so instead of having "Leagues", "Divisions", and "Teams" properties, per se, I needed every object to just have "Options".
eskerber