質問

I am very new to MVVM so hopefully this isnt a hard problem.

Basically I have two Views:- 1. Holds a Data Grid which displays everything inside the ObservableCollection object 2. The second view basically has two text boxes which adds to the ObservableCollection when the user presses OK on the form.

Bascially what I am doing is showing the 2nd view from the 1st view with a button click, button labelled "Add Project"

Then I enter the information I need to add to the ObservableCollection in the 2nd view. When I press OK on the form, It calls a method "AddMProduct" which basically adds a item to the collection inside the ViewModel.

But the problem is by doing this its creating a new ViewModel() object so therefore reinitialise the ObservableCollection. So therefore the collection gets reset back to zero.

So in the MVVM model, how do I basically retain the collection, between the 2 views and the ViewModel?

Thanks

------------------Code---------------

VIEW 1

public partial class MainWindow : Window
{
  public MainWindow()
  {
     InitializeComponent();
  }

  private void btnAddProjectGroup_Click(object sender, RoutedEventArgs e)
  {
   // Open new window here to add a new project group
      AddProductGroup dlg = new AddProductGroup();
     dlg.ShowDialog();
  }
}

VIEW 2

   ProductGroupBindable newProduct = new ProductGroupBindable();
    ProductsViewModel viewModel = null;

    public AddProductGroup()
    {
        viewModel = new ProductsViewModel();

        InitializeComponent();
    }

    private void btnOK_Click(object sender, RoutedEventArgs e)
    {
        newProduct.Id = System.Guid.NewGuid();
        newProduct.Name = txtName.Text;
        newProduct.Description = txtDescription.Text;

        viewModel.AddProduct(newProduct);
        this.Close();
    }

VIEWMODEL

class ProductsViewModel : INotifyPropertyChanged
{
    private ObservableCollection<ProductGroupBindable> m_ProductCollection;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    public ObservableCollection<ProductGroupBindable> ProductCollection
    {

        get
        {
            Test();
            return m_ProductCollection;
        }
        set
        {
            m_ProductCollection = value;
        }

    }

    private void Test()
    {
        m_ProductCollection.Add(new ProductGroupBindable { Description = "etc", Name = "test12" });
    }

    public ProductsViewModel()
    {
        if (m_ProductCollection == null)
            ProductCollection = new ObservableCollection<ProductGroupBindable>();
    }


    public void AddProduct(ProductGroupBindable newProduct)
    {
        ProductCollection.Add(newProduct);
        NotifyPropertyChanged("ProductCollection");
    }
役に立ちましたか?

解決

Consider this simple option to fix the problem. Create an overload constructor of the dialog view that accepts ProductsViewModel as parameter. With that you can pass existing viewmodel object from MainWindow to the dialog, hence avoid instantiating new empty viewmodel :

//in MainWindow
private void btnAddProjectGroup_Click(object sender, RoutedEventArgs e)
{
    //Open new window here to add a new project group
    AddProductGroup dlg = new AddProductGroup(this.viewmodel);
    dlg.ShowDialog();
}


//in AddProductGroup :
public AddProductGroup(ProductsViewModel vm)
{
    viewModel = vm;
    InitializeComponent();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top