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 4036712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:15:25+00:00 2026-05-20T12:15:25+00:00

I have recently start to learn WPF + MVVM. It is possible I have

  • 0

I have recently start to learn WPF + MVVM. It is possible I have the wrong end of the stick or the wrong stick completely with the way things should be done.

I am trying to create a simple order app, it has order items, each order has lineitems. There are two sections on the main page, (to start with they were both usercontrols).

The top is bound to the list of orders. The bottom is bound to the currently selected order, (the idea being it will display the detail of that order).

The details view needs to be able to view / edit and add new items. To this end I thought I should have two views / templates, the edit order view and the new order view / template. During my reading I came across datatemplates and thought this would be a good way to achieve this.

In my mainwindow.xaml I have the list of orders view bound as follows:

<ContentControl Content="{Binding Path=OrdersViewModel}"

The view is hooked up in the resources file with:

<DataTemplate DataType="{x:Type vm:AllOrdersViewModel}">
    <vw:AllOrdersView />
</DataTemplate>

This works great, when the app starts up they orders are displayed. I did the same with the order detail to start with (i.e. it only had the datatype set rather than any key defined. This worked well, if no order was selected then the view would not display, when an order was selected it would show up.

I am currently trying to change this to a template so that I can dynamically switch the template / view so that when they user clicked NEW order (the viewmodel has an Enum that states the current state of an order, so I would check against the enum value), the view should change.

If I try the following it will blow up. The app starts, the list of orders is displayed (at this point no orders are selected), and the details view is displayed, then one of the converters will blow up due to dependencyproperty.unsetvalue, (side note, should I always check for that?). Ideally I do not want the view to be shown until an order is selected:

<ContentControl Content="{Binding Path=OrderViewModel}" ContentTemplate="{StaticResource ordertemplate}" />

   <DataTemplate x:Key="ordertemplate">
       <TextBlock Text="hello" />
    </DataTemplate>

I have two questions mainly:

1) how would I stop the template from being displayed until it has data to display?

2) how could I switch the views / templates dynamically based on an enum?

  • 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-05-20T12:15:25+00:00Added an answer on May 20, 2026 at 12:15 pm

    The most straightforward mechanism to use for this situation would be a DataTemplateSelector, which contains code that you write that can examine the bound object, in this case your ViewModel, and choose the appropriate template to use, including handling the case of no detail selection.

    <ContentControl Content="{Binding Path=OrderViewModel}" 
                    ContentTemplateSelector="{StaticResource MyCustomSelector}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have recently been given a project to complete which uses XML quite extensively.Am looking
I have recently started having problems with TortoiseCVS, or more specifically with plink, the
I have recently installed .net 3.5 SP1. When I deployed a compiled web site
I have recently upgraded some of my web applications to ASP.NET 3.5 by installing
I have recently written an application(vb.net) that stores and allows searching for old council
I have recently started using Vim as my text editor and am currently working
I have recently been doing a bit of investigation into the different types of
I have recently run across these terms few times but I am quite confused
We have recently migrated a large, high demand web application to Tomcat 5.5 from
I have recently been thinking about the difference between the two ways of defining

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.