-2

In my project, I have a class MainViewModel where I have DataContext property and I load MENU in MainWindow from Database using DataContext. In MainWindow, I have a ContentControl to load UserControl. When I click MENU ITEMS, it should calls a function of MainViewModel to load UserControl in the ContentControl. and here I am stuck in a loop.

MainWindow:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = new MainViewModel();            
    }
}

XAML:

<ContentControl x:Name="contentCont1" />

MainViewModel:

public class MainViewModel: ViewModelBase
{
    UCSale ucSale = new UCSale();
    MainWindow mw = new MainWindow(); //IS IT ALLOED ?
    public MainWindow()
    {            
        Window1.contentCont1.Children.Add(ucSale );;
    }
    private void FillMenuItems()
    {
       //LOAD MENU ITEMS FROM DATABASE
    }            
}
logcat
  • 485
  • 12
  • 22
  • How can that sharewindow code even compile? You have a ctor for mainwindow in it. I recommend usin one window - main window to host content other than dialogs. And. Don't use frames and pages. https://social.technet.microsoft.com/wiki/contents/articles/52485.wpf-tips-do-not-use-frame-and-page-for-navigation.aspx – Andy Feb 16 '19 at 14:36
  • I am using MVVM so I can't use one window. That's why I am in stuck – logcat Feb 17 '19 at 12:47
  • 2
    You're not writing true MVVM code. Rename `ShareWindow` to `MainWindowViewModel` or `MainViewModel`. Your `MainWindow` class should not construct or assign `DataContext`. `ShareWindow` should not construct `MainWindow` (that's done inside `App.xaml.cs`). – Dai Feb 18 '19 at 13:08
  • Your code contains many errors, it doesn't compile and doesn't match your description. Please carefully read your question one more time and then edit it in such way that the code illustrates your issue ([mcve]) and is correct. Finally, **state** your question, be clear and concise (there's no question in your post currently). – dymanoid Feb 21 '19 at 10:58
  • @logcat This is not MVVM implementation. You are writing wrong code. In `MainViewModel`, you should take proper properties and then bind them into View. – Gaurang Dave Feb 25 '19 at 06:02

1 Answers1

0

Using an MVVM approach, your ViewModel (ShareWindow) should not know about the View (MainWindow). On top of that, your constructor for MainWindow is:

public MainWindow()
{
    DataContext = new ShareWindow();            
}

Which initialises an instance of ShareWindow.

The construction of ShareWindow involves:

UCSale ucSale = new UCSale();
MainWindow mw = new MainWindow(); //IS IT ALLOED ?
public MainWindow()
{            
    Window1.contentCont1.Children.Add(ucSale );;
}

which is instantiating an instance of MainWindow: MainWindow mw = new MainWindow(); //IS IT ALLOED ? and so a loop is formed.

You can avoid this by simply removing the MainWindow instantiation from the ShareWindow view model as this violates the MVVM pattern. Any comms to the View from the ViewModel should use events to keep them decoupled.

CodexNZ
  • 724
  • 7
  • 15