I have created three sliders to quickly input data into Windows Store APP. Range of each slider is 0-9, all together represents a value from range 000 - 999 in TextBox.
I can calculate the value using following Formula
int result = Slider1.Value * 100 + Slider2.Value * 10 + Slider3.Value;
I can simply attach this to every slider's ValueChanged Event, but there is a more elegant way Via XAML Binding. I created something like this
public class MyViewModel
{
public MyViewModel() { }
public double TotalValue { get; set; }
}
but I cannot access any of sliders. Is there any way to pass them as constructor parameters? Or any other way to perform this binding?
EDIT Adding data context
<Page.DataContext>
<local:MyViewModel/>
</Page.DataContext>
Bind every slider value to a property in the viewmodel:
(make sure to implement INotifyPropertyChanged)
public class MyViewModel:INotifyPropertyChanged
{
public MyViewModel()
{
}
private double valueSlider1;
public double ValueSlider1
{
get { return valueSlider1; }
set
{
valueSlider1 = value;
RaisePropertyChanged("ValueSlider1");
RaisePropertyChanged("TotalValue ");
}
}
private double valueSlider2;
public double ValueSlider2
{
get { return valueSlider2; }
set
{
valueSlider2 = value;
RaisePropertyChanged("ValueSlider2");
RaisePropertyChanged("TotalValue ");
}
}
private double valueSlider3;
public double ValueSlider3
{
get { return valueSlider3; }
set
{
valueSlider3 = value;
RaisePropertyChanged("ValueSlider3");
RaisePropertyChanged("TotalValue ");
}
}
public double TotalValue
{
get { return ValueSlider1*100+ValueSlider2*10+ValueSlider3; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Then, apply the binding to the sliders in this way:
<Slider Value="{Binding SliderValue1, Mode=TwoWay}" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.