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.
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}"/>
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.