Reputation: 1286
I am creating an editor for an object instance hierarchy. The editor has a panel whose child controls vary depending on the fields of the object. So for type A which has an integer field it will have a spinner control; for type B which has a string field it will have a TextBox. And so on.
Question is, how do you achieve this in MVVM?
Upvotes: 0
Views: 1008
Reputation: 427
You can use ContentControl
in your XAML code nad bind to some type (event system types - what you want):
<ContentControl Content="{Binding YourProperty}"/>
(YourProperty is a Property from you ViewModel attached to view)
then you have to create DataTemplate that render view:
<DataTemplate DataType="{x:Type system:int}">
<views:MyWindow/>
</DataTemplate>
system and views are namespaces in you xaml code. In this example I use system:int type, but it can be your custom type. MyWindow
is an UserControl
object - so you basically create another WPF UserControl
file in your solution.
It works like this. ContentControl
gets its Content
and check it's type. It looks for DataTemplate
that can cast this type to some View
(it can be text, textbox, ect) that can be rendered as a Content
.
Best regards
Upvotes: 3