MVVM – View binding directly to model properties? Why not?

Sorry for pasting this inline, but I can't access pastebin at work. Here's a tiny contrived (and incomplete; this computer doesn't have VS on it) WPF example to demonstrate my question.

class Model // normally would implement INPC for better performance but I'm lazy here { public string A {get; set;} public string B {get; set;} } class ViewModel : VMBase //INPC, SetField { public ObservableCollection<Model> Models {get; set;} private Model _SelectedModel; public Model SelectedModel { get { return _SelectedModel; } set { SetField(ref _SelectedModel, value); } } public ViewModel() { Models = new ObservableCollection<Model>(); Models.Add(new Model {A = "Foo", B = "Bar"}); Models.Add(new Model {A = "Baz", B = "Ref"}); } } /* <Window ...> <!-- sets datacontext to instance of ViewModel --> <StackPanel> <ListBox ItemsSource="{Binding Models}" SelectedItem="{Binding SelectedModel}"> <!-- elided: template to concatenate .A and .B and use as display --> </ListBox> <TextBox Binding="{SelectedModel.A, UpdateSourceTrigger=PropertyChanged}" /> <TextBox Binding="{SelectedModel.B, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Binding="{SelectedModel.A}"/> <TextBlock Binding="{SelectedModel.B}"/> </StackPanel> </Window> */ 

When you edit the TextBox contents, the entries in the ListView and TextBlocks update immediately and automatically. Well and good.

Various corners of the internet tell me that I am Doing It Wrong here, by coupling the view directly to the model's properties. From a purist point of view I guess I can see the point; the problem is I'm having trouble envisioning a future maintenance situation where this would actively fuck me over any worse than if I'd had proxy properties in the viewmodel to interact with/bind to instead.

My question then is this: what sort of situations would cause me to wholeheartedly regret binding in this manner? It's proven the easiest way to deal with lists, collections and selected items to me.

by ciaphas01 via /r/csharp

Leave a Reply