1
votes

I am new to MVVM and WPF.

I am using MVVM Light to make an application which contains a DataGrid within a window, which has a view model (MainViewModel) and another window for adding and editing records in the DataGrid, that also has its own view model (EditViewModel).

What I am worried about is the approach I am using to open the Add/Edit window from the MainViewModel. In the MainViewModel I have a property SelectedItem, which is bound to the SelectedItem property of the DataGrid and an IsEdit boolean property that indicates if the Add/Edit window should be launched in Add or Edit mode.

When the Add/Edit window gets opened in edit mode, in the constructor of its view model I have the following line:

 MainViewModel mainViewModel = ServiceLocator.Current.GetInstance<MainViewModel>();

That obviously retrieves the current instance of the MainViewModel, which works perfectly fine, but I am not really sure it is the best way to do this.

Also if I have more than one instances of the Main window, that use the same MainViewModel instance and I open an instance of the Add/Edit window from both of them, the Add/Edit windows are going to get data from the same instance of the MainViewModel which may be a problem.

If I try to create a new instance of MainViewModel for each MainWindow I open, then I don't know how to pass the instance of the currently used MainViewModel to the EditViewModel.

I hope I made clear what I need to do. Tell me if I have missed something and I will add it:)

Thanks in advance

1

1 Answers

0
votes

Hi if I havent misunderstood your problem incorrect you can do it this way: Since i need IsRequired dependency Property in both MainView and EditView i created a class that extends Window class

public class ExtendedWindow:Window
{
    public static readonly DependencyProperty IsRequiredProperty = DependencyProperty.Register("IsRequired", typeof(bool), typeof(ExtendedWindow));

    public bool IsRequired
    {
        get { return (bool)GetValue(IsRequiredProperty); }
        set { SetValue(IsRequiredProperty, value); }
    }
}

MainView and ViewModel

    public partial class MainWindow:ExtendedWindow
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        EditView editView = new EditView();
        **((EditViewModel)editView.DataContext).IsRequired = this.IsRequired;**
        editView.Show();
    }
}

public class MainViewModel : INotifyPropertyChanged
{
    public MainViewModel()
    {
        IsRequired = true;
    }
    private bool isRequired;
    public bool IsRequired
    {
        get { return isRequired; }
        set { isRequired = value; Notify("IsRequired"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

EditView and ViewModel

public partial class EditView:ExtendedWindow
{
    public EditView()
    {
        InitializeComponent();
        DataContext = new EditViewModel();
    }
}

public class EditViewModel : INotifyPropertyChanged
{
    private bool isRequired;
    public bool IsRequired
    {
        get { return isRequired; }
        set { isRequired = value; Notify("IsRequired"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

This is just kind of dummy but can give you idea how you can do it. I have tried it in dummy and its working fine.