Ruben
Ruben

Reputation: 1093

Working with viewmodel, how to change property in other window?

I am sitting with a problem and hope that you can help me.I'm implementing in WPF MVVM. I'm going to explain as good as I can. So i have one view. In which i have one big property Examination. In this view i have to be able to change the settings of the property Examination. This goes fine. But, I want to have a button, and a dialog appears. There I can change some specific (other) settings or properties of Examination. I don't want to do this all in one window so I work with a dialog. I do this in my viewmodel:

private void AlgemeneGegevensClick(object sender, RoutedEventArgs e)
        {
            ToetsBeheerViewModel vm = (ToetsBeheerViewModel)this.DataContext;
            EditAlgemeneGegevens window = new EditAlgemeneGegevens(vm);
            window.Show();

        }

So a window dissapears with some textboxes where i can fill in some specifications of a test. But now my problem is, how do i return this changes (of the examination-object) to the viewmodel? Because I do this in my apart window:

 public partial class EditAlgemeneGegevens : Window
    {
        private ToetsBeheerViewModel toetsb;
        public EditAlgemeneGegevens(ToetsBeheerViewModel vm)
        {
            InitializeComponent();
            toetsb = vm;

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            toetsb.Examination.Course = "blablabla";
        }


    }

So i want to change the Course property of examination in blablabla. But my viewmodel will not know that this property is changed? Hope you get it.. And can help me please :)?

Thanks..

Upvotes: 1

Views: 462

Answers (2)

Dean Kuga
Dean Kuga

Reputation: 12131

IMO, Your dialog window should have it's own viewmodel and than you can just send a message from that viewmodel to the original viewmodel as described here

Upvotes: 0

Jeff
Jeff

Reputation: 36583

It looks like you're passing in the ViewModel to your new window...so shouldn't any changes the new window makes be reflected on that instance?

If you need a more loosely coupled way of returning results, you can consider a message bus infrastructure for your application. There are several implementations, but here's a good article:

http://blog.tonysneed.com/2011/03/03/climb-onboard-on-the-mvvm-message-bus/

Upvotes: 1

Related Questions