Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I dynamically create UserControls on WPF windows?
    text
    copied!<p>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.</p> <p><strong>Some background</strong><br> 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.</p> <p><strong>The issue itself</strong><br> 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.</p> <p><strong>So...</strong><br> 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...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload