2

In MainWindowViewModel I create OrganizationsViewModel for dialog

   OrganizationsVM = new OrganizationsViewModel(this.Organizations);
   DialogService.Instance.ShowDialog(OrganizationsVM);

Now I just have this in constructor

public OrganizationsViewModel(List<Organization> orgs)
{    
      // _organizations = new ObservableCollection<Organization>();
      this.Organizations.Add(new Organization { Code = "1", Name = "test", leader = "leader" }); 
}

Organizations property in ViewModel:

    public ObservableCollection<Organization> Organizations
    {
        get
        {
            if (_organizations == null)
            {
                _organizations = new ObservableCollection<Organization>();
                //this._organizations.Add(new Organization { Code = "1", Name = "test", leader = "leader" }); 

            }
            return _organizations;
        }

        set
        {
            _organizations = value;
            this.OnPropertyChanged("Organizations");
        }
    }

I check that _organizations and Organizations property is not null after creation of object. enter image description here

  public void ShowDialog(OrganizationsViewModel viewModel)
   {
       dialog = new DialogView() { DataContext = viewModel }; 
       dialog.Owner = Application.Current.MainWindow;
       dialog.ShowInTaskbar = false;
       dialog.ShowDialog();
   }

Dialog is working like in this question description wpf data template is not working with contentcontrol in mvvm dialog

After dialog.ShowDialog() call breakpoint on line if (_organizations == null) hits and I check that _organization is null. So I always have empty DataGrid in OrganizationsView.

 <DataGrid   VerticalAlignment="Top" ItemsSource="{Binding Organizations}"/>

enter image description here

But if I uncomment

//this._organizations.Add(new Organization { Code = "1", Name = "test", leader = "leader" });

in Organizations property, then I can see this row in DataGrid, because it creates after ShowDialog method call.

Community
  • 1
  • 1
Lavandil
  • 153
  • 2
  • 5
  • 14

4 Answers4

1

Cannot reproduce but you have to change your constructor in OrganizationsViewModel from:

public OrganizationsViewModel(List<Organization> orgs)
{    
    // _organizations = new ObservableCollection<Organization>();
    this.Organizations.Add(new Organization { Code = "1", Name = "test", leader = "leader" }); 
}

To accept accept ObservableCollection as parameter and not List

public OrganizationsViewModel(ObservableCollection<Organization> orgs)
{
    // _organizations = new ObservableCollection<Organization>();
    // this.Organizations.Add(new Organization { Code = "1", Name = "test", leader = "leader" });
    this.Organizations = orgs;
}

Made available an example based on your scenario at https://github.com/mgigirey/PassDataToDialog. Doesn't show any problem, you can compare.

mgigirey
  • 1,027
  • 9
  • 13
  • Thank you very much for example! In DialogView I have only , and OrganizationView is UserControl that binds to ViewModel in Application.Resources(stackoverflow.com/questions/27295070/…). So I decided to make this changes to your project and see if it would work after them, look to the OrganizationView, which I don't examine before and found my mistake. – Lavandil Dec 10 '14 at 15:59
0

Your constructor code might be the source of your problem as you need to assign the items from the list to the ObservableCollection.

Try this:

_organizations = new ObservableCollection<Organization>(orgs);
toadflakz
  • 7,764
  • 1
  • 27
  • 40
  • I tried this, but still after calling ShowDialog() when execution returns to viewmodel and Organization property is checking _organizations field is already null and observable collection is recreated. To make example simple I just add 1 organization in constructor and comment this line. This is just missprint that I don't pass orgs collection in this line and it does'nt help. – Lavandil Dec 10 '14 at 13:19
0

Have you tried dispatching the call to the constructor and the show dialog to the UI thread directly? It's possible that your constructor is created on one thread and your dialog is on another(UI thread.) Try something like this:

System.Action method = () =>
{
     OrganizationsVM = new OrganizationsVM(this.Organizations);
     DialogService.Instance.ShowDialog(OranizationsVM);                
};

System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(method);       
Chris
  • 31
  • 4
0

I just comment this in OrganizationView and it works rigth. I think that this recreates ViewModel. Thank you for answers.

  <!--<UserControl.DataContext>
        <vm:OrganizationsViewModel />
   </UserControl.DataContext>-->
Lavandil
  • 153
  • 2
  • 5
  • 14