Reputation: 19
I'm trying to make a slider and depending on the value display an image with a switch case. However with this c# code, it goes straight to case 1. Is it possible to wait the user click and drag and once stopped it goes to my switch case ? And also how do i start the slider at a special tick for example 3 ? Thanks in advance! Here is my c# code:
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
switch (Slider.Value.ToString())
{
case "0":
...
case "1":
...
}
}
Here is my xaml for the button specs :
<Slider HorizontalAlignment="Left" Height="29" Margin="474,375,0,0" VerticalAlignment="Top" Width="172" Minimum="0" Maximum="4" IsSnapToTickEnabled="True" TickPlacement="BottomRight" Foreground="Black" Ticks="1,2,3" ValueChanged="Slider_ValueChanged" Name="Slider"/>
Upvotes: 0
Views: 1171
Reputation: 6724
You can "start" the Slider
somewhere by setting Slider.Value
at the start of your code. You can access the Slider
from your code-behind by giving it a name (<Slider Name="Something" ... />
). Usually though, visual elements are controlled by declaring Dependency Properties and then Binding to them. In your case you would bind to theSlider.Value
property.
As for waiting until the user has stopped, you could use the answer from this question: https://stackoverflow.com/a/723547/5086631 - using Thumb.DragCompleted
instead of Slider.ValueChanged
.
<Slider Thumb.DragCompleted="MySlider_DragCompleted" />
Even though this will wait for the user to finish dragging, the downside is that it only works with dragging the thumb, and won't be caused if the user changes the value another way (e.g. with the arrow keys or clicking somewhere along the slider).
If you used a dependency property and a binding instead, you could do something like this:
<Slider Value="{Binding YourProperty, UpdateSourceTrigger=PropertyChanged, Delay=250}" ... />
This adds a 250ms delay to the binding, meaning it will only update after there has been no change for 250ms. It doesn't wait for the user to let go, but it does wait for them to stop moving the slider for a moment before updating, instead of updating after every tiny change.
Upvotes: 2