Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable slider keyboard events

Tags:

c#

.net

wpf

I have this slider in my XAML:

<Slider Width="250" 
        Margin="0,-2,0,0"
        VerticalAlignment="Center"
        Name="TimeSlider"
        Maximum="100"
        Thumb.DragStarted="MouseEnterSlider"
        Thumb.DragCompleted="MouseLeaveSlider"
        ValueChanged="TimeSlider_ValueChanged" />

I noticed the if I press the up/down arrow keys on the keyboard, the slider will change its value.

I tried to disable it with:

TimeSlider.IsEnabled = false;

But this disables the slider completely. Is there a way to only disable the up/down arrow keys?

like image 387
YosiFZ Avatar asked Dec 05 '13 15:12

YosiFZ


2 Answers

Set Focusable to false, like so:

<Slider Width="250" Margin="0,-2,0,0" VerticalAlignment="Center" Name="TimeSlider" Maximum="100" Thumb.DragStarted="MouseEnterSlider" Thumb.DragCompleted="MouseLeaveSlider" ValueChanged="TimeSlider_ValueChanged" Focusable="False" />
like image 118
JMK Avatar answered Sep 21 '22 12:09

JMK


Try using the PreviewKeyDown event on the Control you want to use, and handle it the way you want to handle, and set e.Handled to True, so it won't get to the slider.

like image 28
Programmer Avatar answered Sep 19 '22 12:09

Programmer