This project is read-only.

Are default values suppose to behave this way?

Sep 21, 2011 at 10:51 PM

I have followed the blog post (http://houseofbilz.com/archives/2010/05/08/adventures-in-mvvm-my-viewmodel-base/) and it is recommended that I put default values as the second parameter of the Get(). This works as expected except if I were to put the default value of a new ObservableCollection<T> [e.g. Get(() => MyProp, new ObservableCollection<string>())], it will keep 'newing up' a collection and losing the previous one. I thought the way this was suppose to work was like a constructor in which once it was set, it will keep using that instance initially created, is this correct?

Also on a related note, once default values are placed, isn't the Visual Studio designer suppose to display them? This is for design-time data obviously, but I cannot get the designer to display the properties that were bound with default values. 

This is good framework, and would like to use it more, but these two things bug the heck out of me, unless I'm doing it wrong. :)

Sep 21, 2011 at 11:26 PM

You are confusing "default value" with "initial value".   Do this instead:

Get(() => MyProp, () => new ObservableCollection<string>())

I hesitated adding "initial value" because the getter sets if the value is not there.  Semantically, it is odd... but it also makes some sense for the reason you mentioned, so it is there.

As for getting the properties to show up in the designer, I've not had a problem with it in the past.  Can you give me an example of what you are doing?

Brian

Sep 21, 2011 at 11:42 PM

Gotcha, didn't realize that there was another overload method for initial value. I tried your syntax here's my code:

public ObservableCollection<Pushpin> MapPushpins
{
    get { return Get(() => MapPushpins, () => new ObservableCollection<Pushpin>()); }
    set { Set(() => MapPushpins, value); }
}

But now the compiler gives me this error: 

Cannot convert lambda expression to type 'System.Collections.ObjectModel.ObservableCollection' because it is not a delegate type
First time I'm seeing this error, and I believe if I get this working my DesignTime data should be showing up.

 

Sep 21, 2011 at 11:52 PM
Edited Sep 21, 2011 at 11:57 PM

Odd. It looks like the compiler is failing to figure out which overload to call... I'd hate to have to do it this way, because it will make the code messy, but try this (I don't have a Windows/VS env close by):

Get<ObservableCollection<Pushpin>>(() => MapPushpins, () => new ObservableCollection<Pushpin>());
If that doesn't work, let me know. When I get to a dev box, I'll figure it out. Forgive me here... I haven't written WPF or Silverlight code in about a year. I am a little rusty :)
B

Sep 22, 2011 at 12:29 AM

Brian,

I figured out my problem. I was using the binaries which were uploaded on Jun 11, but the latest patch which adds the "Initial Value" on property getters was released *drum roll* on Jun 18. I hope no one was put away about your work because they were using an outdated version (I was about to :P). As for my DesignTime data, it is still not showing up :/ I believe it has to do with the way I'm resolving my ViewModels. To verify I'm binding correctly, do I have to bind my properties with the [PropertyName] syntax in XAML as you would do with Commands? 

This is off-topic so you don't have to reply to this. I'm using Ninject as my dependency injection framework, and if this application is in DesignTime I need to create a container that does not make use of a Web Service that I'm using in my application, I believe this is my problem and I'll look more into later.

Thanks for your speedy replies! Usually nobody answers if the project is not recently active.