batressc
batressc

Reputation: 1588

No binding Value Property of Slider in Windows Phone 8

I'm new to programming in Windows Phone 8. I am studying "The Binding". I try to bind the property "Value" of a Slider, but when running the application I do not see any change.

The XAML code is this:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <StackPanel>
        <Slider Minimum="1" Maximum="100" Value="{Binding Valor}" />
    </StackPanel>
</Grid>

The ViewModelBase class is this:

public class VMBase : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    public VMBase() {}

    public void RaisePropertyChanged(string PropertyName) {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }
}

The ViewModel class is this:

public class VMSlider : VMBase {
    private int _valor;

    public VMSlider() {
        _valor = 43;
    }

    public int Valor {
        get { return _valor; }
        set { 
            _valor = value;
            RaisePropertyChanged("Valor");
        }
    }
}

In the code-behind class of my XAML y write this:

this.DataContext = new ViewModel.VMSlider();

I need to say why.

Thank you.

Upvotes: 0

Views: 1097

Answers (1)

Silver Solver
Silver Solver

Reputation: 2320

The Value property on the Slider control is of type Double. Your Binding doesn't work because Valor is a value of the wrong type.

You must either implement a Value Converter or change Valor to a Double.

Upvotes: 2

Related Questions