Reputation: 936
I have a question regarding reactiveui view models.
I have two properties on my View Model, Month and Year and wish to have a further output property DateLabel which fill combine the two and format nicely e.g. "Jan 2015".
This third property is dependent on the other two as either change this should be updated and anything bound to this property should also be updated.
Having read the ReactiveUI documentation I figured that an Output Property was what I needed, but I can only get the label to update when one or the other dependent property changes not either. This was achieved with the following code.
readonly ObservableAsPropertyHelper<string> dateLabel;
public string DateLabel {
get { return dateLabel.Value; }
}
this.WhenAnyValue (x => x.Month)
.Select (x => CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName (x) + this.Year)
.ToProperty (this, x => x.DateLabel, out dateLabel);
this.WhenAnyValue (x => x.Year)
.Select (x => CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName (this.Month) + x)
.ToProperty (this, x => x.DateLabel, out dateLabel);
In this example the second WhenAnyValue seems to overwrite the first, so that any changes in Year are reflected in a change in DateLabel but any changes to Month are not, if i switch the order then any changes to Month are reflected and not any changes to Year.
I am sure i am just missing something really fundamental and any help would be much appreciated.
Thanks, Ed
Upvotes: 1
Views: 211
Reputation: 1197
Try the following:
this.WhenAnyValue (x => x.Month, y => y.Year )
.Select (x => String.Format("{0} {1}", CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName (x.Item1), x.Item2))
.ToProperty (this, x => x.DateLabel, out dateLabel);
I think that will do what you want!
Upvotes: 3