MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > Extended canvas KeyDown event not fired
發問發問
 

已答覆Extended canvas KeyDown event not fired

  • Friday, 2 May, 2008 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

解答

  • Monday, 5 May, 2008 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

所有回覆

  • Monday, 5 May, 2008 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

  • Tuesday, 6 May, 2008 7:45Anonymousjtyjtyj 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

     

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

     

    Best regards

    MarN