Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a query that so far I haven't been able to find a solution too.

I have several datatemplates that are to be applied to a ListBox as follows:

XML
<DataTemplate x:Key="ValidationTemplate" DataType="{x:Type meta:CustomerValidationResult}">
                <views:ResultViewCustomerValidation />
            </DataTemplate>

            <DataTemplate x:Key="FileLoadTemplate" DataType="{x:Type meta:FileLoadResult}">
                <views:ResultViewFileLoad />
            </DataTemplate>

            <DataTemplate x:Key="ImportTemplate" DataType="{x:Type meta:CustomerImportResult}">
                <views:ResultViewCustomerImport />
            </DataTemplate>


Each of these is defined in the UserControl.Resources section of a user control and a DataTemplateSelector is applied to a Listbox to choose which to use.

This all works perfectly. However, I would like to introduce a second ValidationTemplate which is a "ResultViewCustomerValidationDetailed" and would like this to be enabled when an item of type "CustomerValidationResult" is selected.

I have read up on these links:
Change WPF DataTemplate for ListBox item if selected[^]
Focus and Selection Styling listbox items with multiple possible templates[^]
A Guided Tour of WPF – Part 4 (Data templates and triggers)[^]
http://blogs.msdn.com/b/wpfsdk/archive/2006/11/07/resource-style-datatemplate-controltemplate.aspx[^]

Thinking that they might point me in the right direction, and if I wasn't using a Template Selector they would work. However I cannot seem to grasp how to do a similar approach when using the Template Selector.

Any ideas?
Posted

1 solution

Try defining your ValidationTemplate as follows:

XML
<DataTemplate x:Key="ValidationTemplate" DataType="{x:Type meta:CustomerValidationResult}">
            <ContentControl Content="{Binding Path=.}">
                <ContentControl.Style>
                    <Style TargetType="{x:Type ContentControl}">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate DataType="{x:Type meta:CustomerValidationResult}">
                                    <views:ResultViewCustomerValidation />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
                                <Setter Property="ContentTemplate">
                                    <Setter.Value>
                                        <DataTemplate DataType="{x:Type meta:CustomerValidationResult}">
                                            <views:ResultViewCustomerValidationDetailed />
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </DataTemplate>


Pay attention to DataTrigger defined in the style of the ContentControl. This trigger binds to the IsSelected property of your view-model. When the value of that property will be true, the detailed template will be applied.

Hope that helps.
Uros
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900