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:
< 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? < 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.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •