Microsoft Developer Network > 포럼 홈 > Windows Presentation Foundation (WPF) > Extended canvas KeyDown event not fired
질문하기질문하기
 

답변됨Extended canvas KeyDown event not fired

  • 2008년 5월 2일 금요일 오후 5: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