Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different views / data template based on member variable

Tags:

I have a view model called

 ViewModelClass  

which contains a boolean.

I have another view model which contains

ObservableCollection<ViewModelClass> m_allProjects; 

Then I have this in my view:

<DataTemplate>    <views:ProjectInfoView x:Key="ProjectInfoDetailTemplate"/> </DataTemplate>  <ItemsControl Grid.Row="1" Grid.Column="0"               ItemsSource="{Binding AllProjects}"               ItemTemplate="{StaticResource ProjectInfoDetailTemplate}"               Margin="10,28.977,10,10"> </ItemsControl > 

I want, based on the boolean in the AllProjects-collection, to use a different datatemplate. What is the best way to do this?

I know I can do this with different ViewModels and use a kind of ViewModel-base object, but I prefer just to use 1 view model.

EDIT:

I want to do this with data triggers. Can someone provide me with some code please?

like image 386
Josh Mulholland Avatar asked Apr 17 '12 12:04

Josh Mulholland


People also ask

What is difference between ItemTemplate and DataTemplate?

You use the ItemTemplate to specify the visualization of the data objects. If your ItemsControl is bound to a collection object and you do not provide specific display instructions using a DataTemplate, the resulting UI of each item is a string representation of each object in the underlying collection.

What is difference between a ControlTemplate and a DataTemplate in WPF?

A ControlTemplate will generally only contain TemplateBinding expressions, binding back to the properties on the control itself, while a DataTemplate will contain standard Binding expressions, binding to the properties of its DataContext (the business/domain object or view model).

What is DataTemplate?

A data template can contain elements that are each bound to a data property along with additional markup that describes layout, color and other appearance. DataTemplate is, basically, used to specify the appearance of data displayed by a control not the appearance of the control itself.

What is WPF TemplateBinding?

A template binding is a special type of data binding that allows you to reference the parent control, read its properties and apply their values. In some cases, you can use the values directly. In other situations you may need to apply value converters or additional formatting.


1 Answers

I usually use a ContentControl to display the data, and swap out the ContentTemplate in a trigger based on the property that changes.

Here's an example I have posted on my blog that swaps a template based on a bound property

<DataTemplate x:Key="PersonTemplate" DataType="{x:Type local:ConsumerViewModel}">      <TextBlock Text="I'm a Person" /> </DataTemplate>   <DataTemplate x:Key="BusinessTemplate" DataType="{x:Type local:ConsumerViewModel}">      <TextBlock Text="I'm a Business" />  </DataTemplate>  <DataTemplate DataType="{x:Type local:ConsumerViewModel}">      <ContentControl Content="{Binding }">          <ContentControl.Style>              <Style TargetType="{x:Type ContentControl}">                  <Setter Property="ContentTemplate" Value="{StaticResource PersonTemplate}" />                  <Style.Triggers>                      <DataTrigger Binding="{Binding ConsumerType}" Value="Business">                          <Setter Property="ContentTemplate" Value="{StaticResource BusinessTemplate}" />                      </DataTrigger>                  </Style.Triggers>              </Style>          </ContentControl.Style>      </ContentControl>  </DataTemplate> 

A DataTemplateSelector will also work, but only if the property that determines which template to show doesn't change since DataTemplateSelectors don't respond to change notifications. I usually avoid them if possible since I also prefer my view selection logic in my view so I can see whats going on.

like image 145
Rachel Avatar answered Oct 18 '22 00:10

Rachel