MSDN > フォーラム ホーム > Windows Presentation Foundation (WPF) > Extended canvas KeyDown event not fired
質問する質問する
 

回答済み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