简体   繁体   English

当滑块聚焦时,KeyDown事件不起作用(C#UWP)

[英]KeyDown Event Does Not Work When A Slider Is Focused (C# UWP)

I am trying to make the focus change away from a slider when the up arrow on the keyboard is pressed. 当按下键盘上的向上箭头时,我试图使焦点从滑块上移开。 The problem is that the up arrow just increases the value of the slider instead of performing the KeyDown event. 问题是向上箭头只是增加滑块的值而不是执行KeyDown事件。 Please help, and here is my code if needed: 请帮忙,如果需要,这是我的代码:

private void slider1_KeyDown(object sender, KeyRoutedEventArgs e)
    {
        if ((e.Key == VirtualKey.Up))
        {
            customvolumebutton.Focus(FocusState.Keyboard);
            label1.Text = "focus changed";
        }
    }

By default, the Up and Down keys control the Value of the Slider . 默认情况下, 向上向下键控制SliderValue If you want your own behavioir, you can manually handle the routed event by calling AddHandler in your page's constructor 如果您想要自己的行为,可以通过在页面的构造函数中调用AddHandler来手动处理路由事件

slider1.AddHandler(KeyDownEvent, new KeyEventHandler(slider1_KeyDown), true);

So just remove your current KeyDown subscription as it's no longer needed. 因此,只需删除当前不再需要的KeyDown订阅。 And what's inside your slider1_KeyDown should now be invoked by any key press. 现在,任何按键都可以调用slider1_KeyDown

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM