question

AbhishekJakabal-9046 avatar image
0 Votes"
AbhishekJakabal-9046 asked NicoZhu-MSFT edited

How to set a slider automatically/dynamically using a particular value?

So i have used a slider to set a particular value which stores it in the database. Now what i want is to change the slider by itself by fetching the value stored in database value.

windows-uwp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you mean update the slider position with database value right?

0 Votes 0 ·

yes exactly

0 Votes 0 ·

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered NicoZhu-MSFT edited

Hello, Welcome to Micorosoft Q&A,

Now what i want is to change the slider by itself by fetching the value stored in database value.

For updating slider value automatically, the better way is using mvvm binding, when you updated the binding property value(SliderValue), the slider will update the slider automatically. For more please refer the following sample code. For more detail please refer to Data binding document

 public class ViewModel : INotifyPropertyChanged
 {
     public event PropertyChangedEventHandler PropertyChanged;
     private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
     {
         if (PropertyChanged != null)
         {
             PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
         }
     }
     private double _sliderValue;
     public double SliderValue { get { return _sliderValue; } set { _sliderValue = value; NotifyPropertyChanged(); } }
 }

Xaml

 <Grid>
     <Grid.DataContext>
         <local:ViewModel x:Name="MyModel" />
     </Grid.DataContext>
     <Slider
         Maximum="150"
         Minimum="20"
         Value="{Binding SliderValue, Mode=TwoWay}" />
 </Grid>

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.