Wpf focus setting issue

Hi I can not focus on the parent control. I have a control that fits on a canvas. If I click on this control, I need to set the focus to the canvas in order to handle some keyboard events. However, despite the fact that I tried to adjust the focus in this way

 protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
           Canvas designer = VisualTreeHelper.GetParent(this) as Canvas;
          designer.Focus() ;//this doesn't work
           Keyboard.Focus(designer); //this also doesn't work


        }

Keyboard events attached to the canvas do not fire.

+3
source share
3 answers

, Canvas Focusable IsEnabled true. Focus() . Focus() docs:

, Focusable IsEnabled .

, PreviewMouseDown, :

 protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
 {
     Canvas designer = VisualTreeHelper.GetParent(this) as Canvas;
     designer.Focus() ;//this doesn't work
     Keyboard.Focus(designer); //this also doesn't work

     // Just in case something else is changing your focus as a result of a mouse event...
     e.Handled = true;
     base.OnPreviewMouseDown(e);
 }
+12

FocusManager, . , : UserControl.

0

My UserControl : Background = "Transparent".

-3

Source: https://habr.com/ru/post/1768991/


All Articles