I have a regular slider and I need to make it so that when the application starts, it takes a zero value, but by default it costs the maximum, how can I fix it? Those. when the application starts it is like this: enter image description here

and should be like this: enter image description here

PS I thought that there is something like StartValue in the properties, but no.


Found what is needed, well, almost. For WPF:
In XAML:

 <Slider Value="0" Minimum="0" Maximum="100" SelectionStart="0" Height="30" IsSelectionRangeEnabled="True" ValueChanged="Slider_ValueChanged" /> 

In the code:

 private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { ((Slider)sender).SelectionEnd=e.NewValue; } 

Actually IsSelectionRangeEnabled : sets the shading of the slider area. If it is set to True, then the starting and ending marks of the shading are set using the SelectionStart and SelectionEnd properties . This is what I needed, but this is in WPF, but how can I do the same thing now, only for Windows Phone?

  • WinForms or WPF? - VladD

1 answer 1

For WPF in XAML:

 <code> <Slider Minimum = 0 Maximum = 100 Value = 50 /> </code> 

Minimum - the minimum value, Maximum - respectively, the maximum, Value - the starting value during initialization. Accordingly, with these parameters, you can use the mechanism of binding.

In windows forms, Slider is called TrackBar and has the same fields for controlling it (Minimum, Maximum, Value).

Here is an example Slider-a from my application:

 <Slider Grid.Column="1" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" Maximum="{Binding Path=ModelMainWindow.SliderMaximum}" Minimum="{Binding Path=ModelMainWindow.SliderMinimum}" Thumb.DragCompleted="Slider_DragCompleted" Thumb.DragStarted="Slider_DragStarted" Value="{Binding Path=ModelMainWindow.SliderValue, Mode=TwoWay}" /> 

This is in model:

  /// <summary> /// Represent minimum slider position /// </summary> public double SliderMinimum { get { return _sliderMinimum; } set { _sliderMinimum = value; NotifyPropertyChanged(); } } /// <summary> /// Represent maximum slider position /// </summary> public double SliderMaximum { get { return _sliderMaximum; } set { _sliderMaximum = value; NotifyPropertyChanged(); } } /// <summary> /// Represent current slider position /// </summary> public double SliderValue { get { return _sliderValue; } set { _sliderValue = value; NotifyPropertyChanged(); } } 

This is the class that implements the INPC (INotifyPropertyChanged) interface:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace MP3_Player.Model { public class BaseModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void NotifyPropertyChanged([CallerMemberName]string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } 

I already inherit my model from it, where I use the implementation of this interface

  • I did it, but nothing changes. - Cocaine Snow
  • If you are programming on WPF, then it is necessary that your bound (binding) values ​​be necessarily either a dependency property or a model (or view model) to implement the INotifyPropertyChanged interface and use it in its properties. (Calling the method RaisePropertyChanged () or NotifyPropertyChanged () can be called differently after assigning a property to new values). If nothing changes immediately upon initialization, look, maybe some of your variables associated with the Slider field are redefined? - Ep1demic
  • okay, right now, try to pohimichit) - Cocaine Snow
  • I added examples in the answer, you can look there, how it is made at me. - Ep1demic
  • Maybe I didn’t explain that way, or maybe I’m doing something wrong. I tried to do the same as yours, but nothing came of it. I tried everything there to "chemize", but also to zero sense. Once again I will try to explain what I need, maybe I just did not express my thought correctly. And so, as soon as I launch the application, I immediately get this Slider and the slider is at the right end and the entire status bar is also painted over, but it is necessary that when the application starts, this slider is on the left side of Slider, as if the beginning of the track is only. Something like this. - Cocaine Snow