0% found this document useful (0 votes)
690 views3 pages

C# - Windows Forms (WinForms) Model View ViewModel Pattern (MVVM) To DataBind or Not - Stack Overflow

The document discusses using the Model-View-ViewModel (MVVM) pattern with Windows Forms (WinForms). It describes how one developer initially tried databinding but found it to be slow with many bound properties. They then rewrote the code to directly subscribe to property change events, which worked faster but resulted in more code. Other responses discuss using a library called Truss that provides WPF-style binding for plain objects, and using a custom BindingSource component to improve databinding performance.

Uploaded by

quinchoponcho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
690 views3 pages

C# - Windows Forms (WinForms) Model View ViewModel Pattern (MVVM) To DataBind or Not - Stack Overflow

The document discusses using the Model-View-ViewModel (MVVM) pattern with Windows Forms (WinForms). It describes how one developer initially tried databinding but found it to be slow with many bound properties. They then rewrote the code to directly subscribe to property change events, which worked faster but resulted in more code. Other responses discuss using a library called Truss that provides WPF-style binding for plain objects, and using a custom BindingSource component to improve databinding performance.

Uploaded by

quinchoponcho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

3/10/2014

c# - Windows Forms (WinForms) Model View ViewModel pattern (MVVM) to DataBind or not - Stack Overflow
sign up

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.

log in

tour

help

careers 2.0

Take the 2-minute tour

Windows Forms (WinForms) Model View ViewModel pattern (MVVM) to DataBind or not

First i'm not mad, because i use MVVM in WinForms-) I know about MVP (Model View Presenter) pattern
and it's variants. But when i started this project i was going to learn WPF and use it, but i'm forced to rush
program development, and have no time to learn WPF, so i have to write it in WinForms which i know well.
So in short i have a large data oriented smart client application, which is close to finish, i have all Models and
ViewModels done (Infrastructure, Domain, Presentation done) UI is done too, now i only need to wire UI to
ViewModels. First i started wiring it using standard winforms way (BindingSources, and simple databinding)
but when i did 30-50% of binding i found out that my program works very slow, i have like 100-150 bound
properties total so far, 30 of them are domain root entity (aggregate root) bindings to it's EditForm. So
databinding doesn't work well in this situation, lots of unnecessary updates, cascade updates of entire view
when something small changes, unclear behavior, and other ugly stuff. It smells like very unreliable code, on
which i have little control. So i began to rewrite wiring as pure clean WinForms code (subscribing to
PropertyChange and ListChanged events, and setting ViewModels property on my own from UI). Lot's of
code to write but it works much faster, i have full control on this, and it feels much more reliable. So what's
your thoughts on this guys? Anyone had such experience? What's your verdict on "To DataBind or Not"?
c#

winforms

data-binding

mvvm

asked Oct 1 '10 at 17:19


Alex Burtsev
4,375 33 58

see also stackoverflow.com/questions/654722/ Ian Ringrose Sep 7 '11 at 14:00

add a comment

3 Answers
You might want to take a look at Truss. It provides a WPF-style binding manager that works on POCOs. It
makes using MVVM with Windows Forms much more effective.
answered Oct 1 '10 at 17:30
Reed Copsey
323k 27 551 913
Very interesting, it's almost the solution i made for myself, i have wrote UI agnostic small databinder manager
myselfm with some reflection stuff but it works only on INotifyPropertyChanged objects so i had to subclass
WinForms controls and override the properties i need, i was going to make some AOP style code injection in
property setters using Spring Framework, but i failed in this not enough time,skill. Do they use some kind of
code injection in setters? Alex Burtsev Oct 1 '10 at 17:45
No - they work directly on POCO's that implement INPC. It's pretty slick. Reed Copsey Oct 1 '10 at 17:51
Nice one, looked at their SRC, same thing i did, my and their sources are pretty close, and usage is same, i
guess hard to implement this other way :-) But as in my case this binding works just on INPC objects, so to bind
it to for example ComboBox.SelectedItem I will have to do MyCombobox : INPC {object SelectedItem {set
{base.SelectedItem = value; OnpropertyChanged} and unfortunately even this way it doesn't work well, lots of
event fire stuff\propety set bugs in WinForms controls, i will have to rewrite them (i tried this too, lots of work, did
some.) Alex Burtsev Oct 1 '10 at 18:25
It would be much nicer to have some interception stuff in property SET method like it is in Nhibernate for
example. Alex Burtsev Oct 1 '10 at 18:26
Interesting thing i found "remade" WinForms controls in updatecontrols.codeplex.com project, they subclass
WinForms controls and add OnGet OnSet handlers in control properties, pity they don't just add

http://stackoverflow.com/questions/3841541/windows-forms-winforms-model-view-viewmodel-pattern-mvvm-to-databind-or-not

1/3

3/10/2014

c# - Windows Forms (WinForms) Model View ViewModel pattern (MVVM) to DataBind or not - Stack Overflow
OnPropertyChanged stuff there. I could rewrite their subclassed controls with INPC to use with Truss.
Alex Burtsev Oct 7 '10 at 7:27
show 4 more comments

Using data-binding in WinForms is really painful, and subscribing to INotifyPropertyChanged events and
doing things manually is overkill. I really like MVVM even on WinForms for it's great testability and
maintainability but not at a price of 3X times more code to write. So for new code I use now combined
View+ViewModel.
answered Nov 27 '10 at 22:21
Alex Burtsev
4,375 33 58
add a comment

Another possibility is to use a inherited BindingSource component for data-binding in WinForms. For
example: http://ingebrigtsen.info/2010/08/31/mvvm-in-windows-forms/. It works smoothly even in NET CF
Environments.
I have modified the implementation to achieve two goals:
an easy databinding support for my ViewModels through WinForms designer
multithreading support with control.Invoke because the default BindingSource doesn't support it. Now it
reacts to PropertyChanged events from a background thread.
Here is my simple ViewModelBindingSource class:
public class ViewModelBindingSource : BindingSource
{
private readonly Control _control = new Control();
private object _viewModel;
private Type _viewModelType;
public ViewModelBindingSource()
{
}
public ViewModelBindingSource(IContainer container)
: base(container)
{
}
public ViewModelBindingSource(object dataSource, string dataMember)
: base(dataSource, dataMember)
{
}
public object ViewModel
{
get { return _viewModel; }
set { _viewModel = value; }
}
public Type ViewModelType
{
get { return _viewModelType; }
set
{
if (value != null)
{
// save the type of our viewmodel
_viewModelType = value;
// create an instance of our viewmodel - so we don't need codebehind
edited Apr 15 at 12:03_viewModel = Activator.CreateInstance(_viewModelType);
answered Mar 31 at 14:55
// add the viewmodel instance to the internal IList collection of
Sven
the bindingsource
183 1 9
Add(_viewModel);
// move to the first element
add a comment
MoveFirst();
// set the datasource of the binding source to the first element

http://stackoverflow.com/questions/3841541/windows-forms-winforms-model-view-viewmodel-pattern-mvvm-to-databind-or-not

2/3

3/10/2014

c# - Windows Forms (WinForms) Model View ViewModel pattern (MVVM) to DataBind or not - Stack Overflow

Not the answer you're looking for? Browse other questions tagged c# winforms
data-binding

mvvm or ask your own question.

http://stackoverflow.com/questions/3841541/windows-forms-winforms-model-view-viewmodel-pattern-mvvm-to-databind-or-not

3/3

You might also like