提出问题提出问题
 

已答复Extended canvas KeyDown event not fired

  • 2008年5月2日 17:01Anonymousjtyjtyj 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi,

     

    In my window I have tabcontainer which includes tabs with my extended canvas.

    I tried in few ways add handlers for events (KeyDown, OnPreviewKeyDown etc, AddHandler(...)) on this canvas but event is not fireing. I tried to use Keyboard.Focus method but it still not works. What should I do? I would like to add some canvas connected functionality which needs keyboard.

     

    Best regard

    MN

答案

  • 2008年5月5日 4:51Wei Zhou - MSFT版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    This is because the Canvas control is not focusable by default. You can set the Canvas.Focusable to true to achieve this goal. Here is the example.

     

    Code Snippet

    <Canvas KeyDown="Canvas_KeyDown" PreviewKeyDown="Canvas_PreviewKeyDown"

           Focusable="True" Width="300" Height="300"/>

    private void Canvas_KeyDown(object sender, KeyEventArgs e)

    {

        Debug.WriteLine("KeyDown");

    }

    private void Canvas_PreviewKeyDown(object sender, KeyEventArgs e)

    {

        Debug.WriteLine("PreviewKeyDown");

    }

     

     

    Best Regards,

    Wei Zhou

全部回复

  • 2008年5月5日 4:51Wei Zhou - MSFT版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    This is because the Canvas control is not focusable by default. You can set the Canvas.Focusable to true to achieve this goal. Here is the example.

     

    Code Snippet

    <Canvas KeyDown="Canvas_KeyDown" PreviewKeyDown="Canvas_PreviewKeyDown"

           Focusable="True" Width="300" Height="300"/>

    private void Canvas_KeyDown(object sender, KeyEventArgs e)

    {

        Debug.WriteLine("KeyDown");

    }

    private void Canvas_PreviewKeyDown(object sender, KeyEventArgs e)

    {

        Debug.WriteLine("PreviewKeyDown");

    }

     

     

    Best Regards,

    Wei Zhou

  • 2008年5月6日 7:45Anonymousjtyjtyj 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

     

    Ok it works but I moved logic of key down event at canvas container level (TabItem).

     

    Best regards

    MarN