tags:

views:

64

answers:

3

Hi,

I'm sure this is a pretty common scenario, and I would like to know how MVVM developers tackle this.

I have a ViewModel that is instantiated on demand, and persists until it is explicitly removed by the user. It's corresponding View is loaded onto the UI on demand. The View is able to unload and it's ViewModel may still exists in the application.

In my scenario, I have a ListBox of preset colors in the View(by setting it's ItemsSource to a Xaml-defined ObservableCollection of SolidColorBrush).

I bound the ListBox's SelectedItem property to a property in the ViewModel so that when the View is to be loaded again, the SelectedItem correctly shows the last selected item in the ListBox and also when the user selects a different color, the VM will handle the change.

My question is, how would u set the default value, say the third item in the ObservableCollection of SolidColorBrush to the ViewModel when the View is first loaded?

A: 

typically In MVVM your ObservableCollection of SolidBrushColor should be defined as a property in ViewModal. and this property should be bound to your ListBox. Now, to set default value you can have it in your ViewModal's constructor

ajay_whiz
as i've explained in my reply with akapoor, my ObservableCollection is defined at the View Level, not ViewModel level, that is why i would like to know how you guys would tackle this?
icube
A: 

If the property bound to SelectedItem is declared as dependency property then you can set the default value of the dependency propery to whatever you want.

 public static readonly DependencyProperty SelectedColorProperty
   = DependencyProperty.Register("SelectedColor", typeof(SolidBrushColor ), 
                                                typeof(<yourviewmodel>),
                                                new UIPropertyMetadata(GetDefaultColor()));

  Where GetDefaultColor() is a static method in your ViewModel, 
  which will return the required color from your ObservableCollection .
akapoor
i'm using MVVM-light actually, therefore i'm not using DependencyProperty but using the INotifyPropertyChange interface. But that said, I get what you meant but the scenario here is my ObservableCollection is defined in the View level not in the ViewModel as this is a Color collection, the Designer is able to modify the Preset Colors on his side, and also able to set the Default Color on his side.
icube
In that case either of following can be useful 1. Pass the default value as parameter to your view model constructor 2. Use Converter and ConverterParameter in xaml where you are binding ListBox SelectedItem to ViewModel Property.
akapoor
A: 

I believe you error is in your implementation. The reason to have MVVM is to have a "separation of concerns". That makes your view just an implementation, that can get switched or updated out if/when the need arises. Once you start putting stuff in your view that is part of the application logic, you are traveling down a path of a maintenance headache, and then spaghetti code can quickly ensue.

Some people say, "Don't put any code in your view", I agree 99% of the time. I say "Don't put any domain/application/business logic in your view."

Whenever you're trying to put some code into your view ask yourself "If I switched from WPF to another framework would my app still work?" If the answer is no, then modify your ViewModel to incorporate what you were trying to put in your view.

Jose
Hi, Thanks for your POV. I have no intention of putting business logic in my View. I'm not sure where did you read on my comments that I'm putting code in my View... hmm? I also strongly believe the "separation of concerns" policy... in my implementation of MVVM, my ViewModel searches for it's corresponding View, my View has no command on it's creation of it's ViewModel. That is why i mentioned that my View may be destroyed but it's ViewModel still remains in memory. And when the user requests for it's View again, the View populates the last set properties on the ViewModel.
icube