I have a TwoWay binding that controls the value of a slider:
<Slider Orientation="Vertical" Height="200" Value="{Binding Path=MapScale, Mode=TwoWay}" Maximum="{Binding MaxScale}" Minimum="{Binding MinScale}" StepFrequency="0.1" />
The binding is in the ViewModel as a DependencyProperty:
public static readonly DependencyProperty MapScaleProperty =
DependencyProperty.Register("MapScale", typeof(Double?), typeof(MappingPageViewModel), new PropertyMetadata(0.0));
public Double? MapScale
{
get { return GetValue(MapScaleProperty) as Double?; }
set { SetValue(MapScaleProperty, value); OnPropertyChanged("MapScale"); }
}
As the code is now, the slider updates properly when I update MapScale (e.g. MapScale += .1). But, if I remove the OnPropertyChanged method (which I was under the impression SetValue already calls), the slider doesn’t update properly.
What have I missed?
Slider.Value is of type
double, notNullable<double>. I’ve noticed that WinRT is very picky about matching binding types. It doesn’t auto-convert most things for you like WPF and Silverlight.My answer is:
Use
INotifyPropertyChangedas @Adi already mentioned AND usedoubleas the property type.If you’re stuck using
Nullable<double>in your view-model, then create anIValueConverterthat converts null to zero.