Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7810999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:59:51+00:00 2026-06-02T03:59:51+00:00

I have a view model called ViewModelClass which contains a boolean. I have another

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:59:53+00:00Added an answer on June 2, 2026 at 3:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model with a property Fields which is an ObservableCollection<FieldVM> .
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have a custom struct called TimeOfDay which is used in a view model
I have a view model that contains a Product class type and an IEnumerable<
I have the following view: @model MyModel1 @{ ViewBag.Title = Index; Layout = ~/Views/Shared/_Layout.cshtml;
i have created the folloiwng view model class public class viewmodelclass { public IEnumerable<Question>
Using an MVVM approach to WPF, I have a view model class called SubButton
I have a business model called Customer which has many required properties (via DataAnnotations)
Suppose I have a partial view called UserDetails whose @model clause is set to
I'll just jump right into it. I have a model called Request which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.