Maximilian max Sauer
Maximilian max Sauer

Reputation: 33

reuse an usercontrol(View) with different ViewModels

I'll go directly to my issue. :)

I use Caliburn.Micro and mvvm. 16 Usercontrols are on my MainView. All are looking the same with some buttons, labels,… like that:

<UserControl x:Class=" Projectxy.usercontrolexample01View"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Projectxy"
         mc:Ignorable="d" 
         d:DesignHeight="200" d:DesignWidth="305"> 
<Grid>
<Button x:Name="button" Content="Button" Width="75"/>
<Label x:Name="label" Content="Label"/>
<ComboBox x:Name="comboBox"/>
</Grid>
</UserControl>

I call the usercontrol in the MainView like:

<ContentControl Name=" usercontrolexample01Model" Grid.Column="2" Grid.Row="1"/>
<ContentControl Name=" usercontrolexample02Model" Grid.Column="2" Grid.Row="2"/>

(I don't plot the Bootstrapper because I think it's not necessary for my question...?)

What I want is not to add 16 times the same xaml code in my project like that:

usercontrolexample01View.xaml
usercontrolexample01ViewModel.cs

usercontrolexample02View.xaml
usercontrolexample02ViewModel.cs

…

It is hard to change 16 xaml files if I want to change something...

I want 16 Views in my MainView and 16 ViewModels (for every time I reuse it in the MainView) but "behind" the 16 Views should be just 1 xaml-file which should be reused(usercontrolexampleView.xaml).

usercontrolexampleView.xaml
usercontrolexample01ViewModel.cs
usercontrolexample02ViewModel.cs
usercontrolexample03ViewModel.cs

Is there any way to reuse 1 Usercontrol (xaml-file) with Caliburn.Micro? I didn’t find some articles about that... :( (There are some with the same titel but they have other focuses like this thread.) Finaly I found a question which is more in my direction.... But how could I manage that with Caliburn? Maybe someone have a code snipped with explanations for me?

I hope my question is clear. I'm just new to stackoverflow, wpf and caliburn micro... And not that familiar with english... :(

Thanks for all suggestions!

Upvotes: 3

Views: 908

Answers (1)

EluciusFTW
EluciusFTW

Reputation: 2615

Caliburn.Micro picks views for view models based on a naming convention. If you have a view model class (it should be in a file in the <appfolder>/ViewModels/ subfolder and named <name>ViewModel.cs), it will look for a view called 'View.xaml' user control in the <appfodler>/Views/ subfolder, and if it finds it, use it for displaying the view model.

In your case, you can have as many instances of your particular view model in your main view model, and then bind them to a ContentControl - each will use the same, once defined view, because they are of the same type.

E.g.: Say your view model is called SubViewModel, and you have created a view for it, called SubView.xaml. In main view model, define arbitrary number of instances,

public SubViewModel sVM1 {get;set;}
public SubViewModel sVM2 {get;set;}

(or even make a List<SubviewModel> of them), and in the main view, place them where you like,

...
<ContentControl Name="sVM1"/>
....
<ContentControl Name="sVM2"/>

then they will all be displayed using the SubView.xaml. (Also note that there is another binding covention here in place: By naming the ContentControl the same as the SubViewModel instances, they are automatically bound by caliburn.micro).

Upvotes: 2

Related Questions