Mazze

Hi!

I have troubles subscribing to the GotFocus event on shapes because it doesn't fire up even though I set the ZIndex to the max value possible:

Line line = new Line();

line.Focusable = true;

line.GotFocus += new RoutedEventHandler(line_GotFocus);

canvas.Children.Add(line);

Canvas.SetZIndex(line, Int32.MaxValue);

void line_GotFocus(object sender, RoutedEventArgs e)

{

// Doesn't show up!

MessageBox.Show("Line got focus!");

}

In contrast to shapes, the GotFocus event seems to be working fine on thumbs:

Thumb thumb = new Thumb();

thumb.Focusable = true;

canvas.Children.Add(thumb);

thumb.GotFocus += new RoutedEventHandler(thumb_GotFocus);

void thumb_GotFocus(object sender, RoutedEventArgs e)

{

// Does show up!

MessageBox.Show("Thumb got focus!");

}

Why does the GotFocus event fire up on thumbs but not on shapes Any help is highly appreciated. Thx!

Regards

Mazze



Re: Windows Presentation Foundation (WPF) GotFocus event doesn't fire up on shapes

Benny Tops

Hi,

The problem hides in the way you're trying to give the line focus. If you would give the line focus, by for example clicking a button and then executing line.Focus() in the event handler, the event will fire.

Best regards,

Benny





Re: Windows Presentation Foundation (WPF) GotFocus event doesn't fire up on shapes

Mazze

Hi Benny,

Thanks for your quick reply. Giving the line focus on a button click is exactly what I did to work around this problem ;-) But in my understanding the line should receive focus automatically when it is being clicked upon because that's exactly the way the thumb and some other controls behave. Anyways, thanks for your help!

Take care...

Mazze





Re: Windows Presentation Foundation (WPF) GotFocus event doesn't fire up on shapes

Benny Tops

Hi,

Line inherits from Shape. Thumb inherits from Control, which is the base class for all user-interactive elements. So, Line is not a WPF Control. It does not even have a Click event. Even if the Line would have Focus, it would be logical focus, not keyboard focus. To select a line, you could use hittesting.

Best regards,

Benny





Re: Windows Presentation Foundation (WPF) GotFocus event doesn't fire up on shapes

Benny Tops

... or you put the line.Focus() in the LeftMouseButtonDown event of the line, which would emulate the behavior of a Control.

Regards,

Benny





Re: Windows Presentation Foundation (WPF) GotFocus event doesn't fire up on shapes

Mazze

Wow,

Hit testing rocks! Until now I could not have been bothered to dive into this topic, but now that I see the potential of this feature I start wondering why I didn't take a look on it before. Thanks for inspiring me!

Regards

Mazze