Lavandil
Lavandil

Reputation: 153

How to pass data to wpf mvvm dialog?

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.

Upvotes: 2

Views: 2814

Answers (4)

Lavandil
Lavandil

Reputation: 153

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>-->

Upvotes: 0

Chris
Chris

Reputation: 31

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);       

Upvotes: 0

mgigirey
mgigirey

Reputation: 1103

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.

Upvotes: 1

toadflakz
toadflakz

Reputation: 7942

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);

Upvotes: 0

Related Questions