Need good way to handle a wpf UC GotFocus and LostFocus events

M

moondaddy

I have a c# 3.5 wpf app which uses user controls for data entry screens.
These data entry screens can be nested inside of each. We can also have
several ones open side by side at the same time. All are in a single
window. My challenge is to know when a data entry screen (UC) becomes the
active control, and when the active user control looses focus and when the
next one gets the focus, and so on. When a user control gets or looses
focus, I want to configure buttons in the window's toolbar according to the
user control that currently has the focus and its current state. In the
winforms days, I could use the user control's Enter and Leave events which
worked very well. In this case I tried to use the user control's GotFocus
and LostFocus events which doesn't work very well at all



this.Focusable = true;

this.GotFocus += new RoutedEventHandler(ucRole_LUProps_GotFocus);

this.LostFocus += new RoutedEventHandler(ucRole_LUProps_LostFocus);



The GotFocus event only fires when I click in a control such as a textbox or
combobox in the user control. It doesn't fire when I mouse click on the
user control it's self or when I tab into it. Furthermore, The Lostfocus
event never fires.



Can anyone recommend a good solution to the problem?



Thanks.
 
Z

Zhi-Xin Ye [MSFT]

Hi moondaddy,

As I understand, you create an UserControl in WPF, and put some controls on
the UserControl, you want to know when the UserControl get focused, and
when it lost focus.

When click the UserControl itself by mouse, the UserControl does not get
focused, you can check this my checking the UserControl.IsFocused property.
So when click the UserControl itself by mouse, the GotFocus event won't
fire. However, you can handle the UserControl.MouseDown event, and call
the Focus() method to force the UserControl to get focused, for example:

void UserControl1_MouseDown(object sender, MouseButtonEventArgs e)
{
this.Focus();
}

From you description, when you tab into the UserControl, the GotFocus event
does not fire, and the LostFocus event never fire as well. From my
experience, these events should fire properly, anyway, I can help you to
find out the problem if you send me a sample code to reproduce this
problem. My email is (e-mail address removed).

I look forward to hearing from you.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can

improve the support we provide to you. Please feel free to let my manager
know what you think of the level

of service provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from

the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take

approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The

offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis

issues. Issues of this nature are best handled working with a dedicated
Microsoft Support Engineer by

contacting Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?

target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top