Add new Views and Viewmodels in Caliburn.Micro?

perfect code 271 Reputation points
2020-06-08T19:25:09.04+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,769 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2020-06-09T09:02:52.53+00:00

    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>
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. perfect code 271 Reputation points
    2020-06-09T12:58:50.967+00:00

    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.