Binding ViewModel to multiple windows

后端 未结 4 820
醉话见心
醉话见心 2021-02-14 21:37

I\'m re-writing my windows forms project that does scoring for sheep shearing events (don\'t ask, its a huge sport in New Zealand) from vbnet to wpf c# and have struck a problem

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-14 21:52

    Your viewmodel defines a static method to get a single instance but you do not use it to instantiate it. Currently your viewmodel is being created with the default constructor which means the two windows will have separate copies.

    Create your viewmodel in the code behind either below InitializeComponent or in the OnNavigatedToEvent.

    Here's some code to explain further:

    define a ViewModel property like so in both windows

    property SheepViewModel ViewModel { get; set; }
    

    Then your constructor:

    public MainWindow()
    {
        InitializeComponent();
        ViewModel = SheepViewModel.GetDetails(); // include this line
        ScoreScreen SW = new ScoreScreen();
        SW.Show();
    }
    

    also remove the

    
    

    from the xaml as it's not required.

提交回复
热议问题