ComplexGroupConverter

The ComplexGroupConverter works in conjunction with the MultiBinding markup to allow you to specify a binding to multiple collections, each collection is treated as a unique data item on the next level of the hierarchy, allowing you to specify HierarchicalDataTemplate markup to format a unique look for each collection.

The following is an example of the use of the ComplexGroupConverter:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:complex="clr-namespace:DaisleyHarrison.WPF.ComplexDataTemplates;assembly=ComplexDataTemplates">
    <Window.Resources>
        <complex:ComplexGroupConverter x:Key="complex-group-converter"/>
...
        <HierarchicalDataTemplate DataType="{x:Type local:ClassA}">
            <HierarchicalDataTemplate.ItemsSource>
                <MultiBinding Converter="{StaticResource complex-group-converter}">
                    <Binding Path="ListOfClassB"/>
                    <Binding Path="ListOfClassC"/>
                </MultiBinding>
            </HierarchicalDataTemplate.ItemsSource>
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/classA.jpg" VerticalAlignment="Center"  Margin="0,5,0,5"/>
                <TextBlock Text="{Binding Path=Label}" ToolTip="{Binding Path=ToolTip}" VerticalAlignment="Center"  Margin="5,0,0,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>
...

In the above example, the next level of the hierarchy could be formated with the following HeirarchicalDataTemplate markup:

...
        <HierarchicalDataTemplate x:Key="IEnumerable[ClassB]" ItemsSource="{Binding Path=.}">
            <TextBlock>List of Class Bs</TextBlock>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="IEnumerable[ClassC]" ItemsSource="{Binding Path=.}">
            <TextBlock>List of Class Cs</TextBlock>
        </HierarchicalDataTemplate>
...

Note that the resource keys used are create by the ComplexGroupDataTemplateSelector using the string format expression from the GroupDataTemplateFormat property.

If you would like more control of the resource keys used on the next level you can specify the key names in the ConverterParameter property. This is a comma delimited list of resource key names. There should be one resource key name specified for each Binding in the MultiBinding markup.
For example:

...
        <HierarchicalDataTemplate DataType="{x:Type local:ClassC}">
            <HierarchicalDataTemplate.ItemsSource>
                <MultiBinding Converter="{StaticResource complex-group-converter}" ConverterParameter="Pickles-Template,IceCream-Template">
                    <Binding Path="Pickles"/>
                    <Binding Path="IceCream"/>
                </MultiBinding>
            </HierarchicalDataTemplate.ItemsSource>
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/classC.jpg" VerticalAlignment="Center" Margin="0,5,0,5"/>
                <TextBlock  VerticalAlignment="Center"  Margin="5,0,0,0">Class C:</TextBlock>
                <TextBlock Text="{Binding Path=Label}" ToolTip="{Binding Path=ToolTip}"  VerticalAlignment="Center"  Margin="5,0,0,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>
...

In the above example, the next level of the hierarchy could be formated with the following HeiarchicalDataTemplate markup:

...
         <HierarchicalDataTemplate x:Key="Pickles-Template" ItemsSource="{Binding Path=.}">
            <TextBlock>Pickles</TextBlock>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="IceCream-Template" ItemsSource="{Binding Path=.}">
            <TextBlock>Ice Cream</TextBlock>
        </HierarchicalDataTemplate>
...

Last edited Dec 1, 2008 at 3:18 AM by aarondh, version 9

Comments

No comments yet.