How should I dynamically create UserControls on WPF windows?
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...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
精彩评论