question

perfectcode-6746 avatar image
0 Votes"
perfectcode-6746 asked DaisyTian-1203 commented

Add new Views and Viewmodels in Caliburn.Micro?

I would like to use Caliburn.Micro in my new MVVM projects. After reading the documentation, I understood that Caliburn.Micro takes care of the connection between views and view models on its own (if of course the naming convention is followed).

For example, this works great with "ShellView" and "ShellViewModel":

     xaml        
             <TextBox x:Name="CustomerCode"/>
        
     viewmodel
             private string _CustomerCode = "Google";
             public string CustomerCode
             {
                 get { return _CustomerCode; }
                 set
                 {
                     _CustomerCode = value;
                     NotifyOfPropertyChange(() => CustomerCode);
                 }
             }

Now when I add new usercontrols (views) to "ShellView" that have their own ViewModels, it no longer works even though I observe naming convention!

example:

in ShellView xaml
<views:DashboardView x:Name="Dashboard"/>

in DashboardView
<TextBox x:Name="CustomerCode"/>

and in DashboardViewModel
private string _CustomerCode = "Google";
public string CustomerCode
{
get { return _CustomerCode; }
set
{
_CustomerCode = value;
NotifyOfPropertyChange(() => CustomerCode);
}
}

However, the texbox "CustomerCode" remains empty.

What have I forgotten?

Thanks

pc

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered DaisyTian-1203 edited

Add the below code in your ShellViewModel:

 public void ShowDashboardView()
         {
             ActivateItem(new DashboardViewModel());
         }

Add the Button named ShowDashboardView and ContextContent named ActivateItem in the ShellView.xaml

  <TextBox x:Name="CustomerCode" />
  <Button Content="Show Dashboard" x:Name="ShowDashboard"  Width="150" Height="50"></Button>
   <ContentControl x:Name="ActiveItem"></ContentControl>

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

perfectcode-6746 avatar image
0 Votes"
perfectcode-6746 answered DaisyTian-1203 commented

Hi DaisyTian-MSFT and thank you very much.

Unfortunately, the ViewModel still does not pass a value from view to view.

Does it need anything else?

What about DataContex, must this be set for view so that view knows which ViewModel to use (I don't think so because it should work by naming convention)?

regards
pc

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have updated my answer for you. There is also another thread to show different Views in one View using Caliburn.Micro for you to refer to.


1 Vote 1 ·