Reusing usercontrol (View) with different ViewModels

I'll go directly to my problem. :)

I am using Caliburn.Micro and mvvm. 16 Usercontrols are in my MainView. They all look the same with some buttons, labels, ... like this:

<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 am calling usercontrol on MainView like:

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

      

(I am not building a Bootstrapper because I think it is not necessary for my question ...?)

I want not to add 16x xaml code to my project:

usercontrolexample01View.xaml
usercontrolexample01ViewModel.cs

usercontrolexample02View.xaml
usercontrolexample02ViewModel.cs


      

Difficult to change 16 xaml files if I want to change something ...

I want 16 views in my MainView and 16 ViewModels (every time I reuse it in the MainView), but there should only be 1 xaml file "behind" the 16 views to be reused (usercontrolexampleView.xaml).

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

      

Is there a way to reuse 1 Usercontrol (xaml file) using Caliburn.Micro? I didn't find any articles about this ... :( (There are some with the same till, but they have other tricks like this thread.) Finally, I found a question that is more in my direction .... But how could I to deal with Caliburn? Maybe someone has some code clipped with explanations for me?

Hope my question is clear. I'm just new to stackoverflow, wpf and caliburn micro ... And not one familiar with English ... :(

Thanks for all the suggestions!

+3
c # wpf user-controls caliburn.micro


source to share


1 answer


Caliburn.Micro

selects views for view models based on a naming convention. If you have a viewmodel class (it must be in a file in a subfolder <appfolder>/ViewModels/

and named as <name>ViewModel.cs

), it will look for a view called "View.xaml" of the custom control in the subfolder <appfodler>/Views/

, and if it finds it, it uses it to display the viewmodel .

In your case, you can have as many instances of your specific view model in your main view model and then bind them to ContentControl

- each will use the same, specific specific view, because they are of the same type.

For example: say your view model is called SubViewModel

and you created a view for it called SubView.xaml

. In the main view model, define an arbitrary number of instances,

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

      



(or even make of them List<SubviewModel>

) and in the main view, put them where you like,

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

      

then they will all be displayed with SubView.xaml

. (Also note that there is another mandatory requirement here: by naming the ContentControl

same as the instances SubViewModel

, they are automatically linked Caliburn.Micro

.)

+2


source to share







All Articles