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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:06:33+00:00 2026-05-22T03:06:33+00:00

I’m working with some WPF stuff at the moment. In the process of coalescing

  • 0

I’m working with some WPF stuff at the moment. In the process of coalescing three very similar forms into one, I’ve come up with an issue. The code that I’ve written works, but it seems far from the best solution, so I thought I’d ask a wider audience for feedback and/or better solutions.

Some background
The three forms share a lot of XAML and code-behind, but there is a section in each form that is different. I’ve retained all the shared code in one form, and split off the parts that are different into UserControls, with the intention of dynamically loading one of them into a container at runtime. We’re using MVVM, so the form has a ViewModel, as do the new UserControls. Naturally, the form’s ViewModel acts as parent and contains the ViewModels of the new UserControls as children.

The issue itself
Initially, I used a ContentControl with a DataTemplateSelector to choose from a number of DataTemplates that each contained one of the UserControls. A property from the parent view model, bound to the ContentControl’s Content property, was used in the DataTemplateSelector to select which DataTemplate to use. As I wasn’t overly enamoured of having to set the Content property, I switched to using a Style with DataTriggers on the ContentControl, doing pretty much the same task. However, I need to be able to traverse the logical tree (for error checking, if you’re wondering), and setting the ContentTemplate with either method doesn’t seem to permit me to continue along the logical tree into the UserControl from the ContentControl once it’s loaded. The UserControl is visible in the container, and has the right ViewModel associated with it through it’s DataTemplate definition, but the ContentControl has no logical children because the Content property is null. So, the solution I’ve fallen back to is checking the relevant property on the parent view model in the form’s constructor, and explicitly instantiating one of the UserControls into the ContentControl’s Content property. Once this is done, I can walk the logical tree into the UserControl. It seems there must be a better way of doing things than this, perhaps some way of doing it all in the XAML.

So…
Is there a better way of dynamically selecting and instantiating one of my UserControls? Does my basic technique of having the parts of the form that are different split off into UserControls sound logical? It seems like this is something that should be fairly common-place, re-using a window but changing certain parts of it, but I didn’t find much on my travels with Google. Perhaps I wasn’t looking in the right places…

  • 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-22T03:06:33+00:00Added an answer on May 22, 2026 at 3:06 am

    I think you should be looking at the VisualTree for the Content, not your LogicalTree.

    Also, I couldn’t really tell from your question above, but it sounded like you might be looking your ContentTemplate for your Content. If that is the case, it won’t work because it’s a template, so it doesn’t actually contain the content. It’s like looking at the Cookie Cutter to find the Cookie.

    And as a side note, I prefer to use DataTemplates than DataTriggers. You don’t need a DataTemplateSelector either

    In my ParentViewModel I’ll have a property like this:

    private ViewModelBase _currentContent;
    public ViewModelBase CurrentContent 
    {
        get {return _currentContent;}
        set
        {
            if (value != _currentContent)
            {
                _currentContent = value;
                OnPropertyChanged("CurrentContent");
            }
        }
    }
    

    and in the XAML for the ParentViewModel I would have something like

    <ContentControl Content="{Binding CurrentContent}" />
    

    Then I’d define my data templates for the different sub-viewmodel types

    <DataTemplate DataType="{x:Type local:SubViewModelA}">
        <local:ViewA />
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:SubViewModelB}" />
        <local:ViewB />
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:SubViewModelC}" />
        <local:ViewC />
    </DataTemplate>
    

    To switch content, I’d change the master’s CurrentContent property to whatever SubViewModel should be displayed

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker

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.