WPF: OnKeyDown() not being called for space key in control derived from WPF TextBox

后端 未结 4 1801
谎友^
谎友^ 2020-12-06 00:39

In a WPF application, I have a control that I have derived from TextBox like this:

public class SelectableTextBlock : TextBox
{
    protected override void O         


        
4条回答
  •  孤街浪徒
    2020-12-06 01:23

    The PreviewKeyDown event exists exactly for this sort of thing.

    private void spacebarHandler_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Space)
            e.Handled = true;
    }
    

    Your KeyDown handler will never receive the KeyDown event for spacebar.

提交回复
热议问题