2

The MVVM (Model-View-ViewModel) pattern is usually used in WPF applications to provide a convenient way to use the databinding feature very efficiently.

I am using the MVVM pattern in a Windows Forms application which works out quite well so far. However in some cases I have an issue with databinding on Windows Forms controls: Say i bind the Value Property of a standard date-time picker to the CurrentDate Property of my ViewModel via

dtpMyDateTimePicker.DataBindings.Add("Value", mViewModel, "CurrentDate") 

, the Property is only updated when the control looses focus. Looking at how the dateTime picker works, by clicking on the "Arrow" symbol you can open a calender and scroll through months/years. While i would like to update my form constantly when scrolling through different dates the databinding only updates the Property of my viewModel when i select a date and close the calendar tool.

A more simple example would be databinding on the Text Property of a Textbox (imagine a search field) - the method for databinding which i am currently using does not allow to constantly update my form while typing.

Obviously i can implement the _ValueChanged events of the controls on my Form and update the Properties of the ViewModel manually. However this takes the whole magic out of the MVVM pattern since i am now only using one-way databinding (ViewModel -> View) and need to implement logic (change events) in the form again.

TLDR: So basically i am looking for a way to specify some kind of UpdateSourceTrigger in Windows Forms applications.

Do you have any suggestions how to do this "properly"?

Thank you in advance!

1 Answer 1

3

The equivalent UpdateSourceTrigger in Windows Forms is DataSourceUpdateMode. You can specify this for a separate Binding (and add this Binding to DataBindings) or you can also specify this via another overload of DataBindings.Add() method which accepts the fifth argument as DataSourceUpdateMode:

dtpMyDateTimePicker.DataBindings.Add("Value", mViewModel, "CurrentDate", true, DataSourceUpdateMode.OnPropertyChanged); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.