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

Abhishek Jakabal 86 Reputation points
2021-09-28T06:59:41.463+00:00

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.

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,851 Reputation points
    2021-09-29T03:18:46.733+00:00

    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.

    0 comments No comments