开发者

How to unfocus TextBox without focusing something else

开发者 https://www.devze.com 2023-03-17 01:53 出处:网络
I believe that when my application starts nothing is focused. At least it looks like it. I would like to add event handler to GotFocus event so control shows popup and loses focus, so popup can be sho

I believe that when my application starts nothing is focused. At least it looks like it. I would like to add event handler to GotFocus event so control shows popup and loses focus, so popup can be shown again without manually removing focus and setting it back. Also it is not needed to have my textbox in focused visual state cause its serving no use to user (this can be changed by template though but answer on this question resolves both issues).

If I am mistaken with my initial observation and something is always focused in silverlight I would like to know what to focus so it looks like nothing is focused (like when app starts). If there is a way to remove visible focus completely - such approach will be better.

EDIT: Actually in my case it turned out a control that had no visualstyle difference when focused was focused at the start of application. I didn't find anything smarter to do then to focus it in my Unfocus() related method.. To go a bit further I can recommend hitting [enter开发者_Go百科] key and see what happens, in my case it also changed focused control state, that looked like its unfocused.

Note taken : in Silverlight there is no "nothing focused state"


Focus in Silverlight is notoriously difficult to manage.

There are lots of controls that can have focus that show no visual difference when they have focus - very different from WinForms.

I've found it useful the following class useful in some of my Silverlight apps to try to figure out focus problems:

public static class WatchWhatsGotFocus
{
    private static DispatcherTimer t;

    public static void StartWatching()
    {
        t = new DispatcherTimer();
        t.Interval = TimeSpan.FromMilliseconds(500);
        t.Tick += t_Tick;
        t.Start();
    }

    public static void StopWatching()
    {
        if (t != null)
        {
            t.Stop();
            t = null;
        }
    }

    static void t_Tick(object sender, EventArgs e)
    {
        var element = FocusManager.GetFocusedElement();            
        if (element != null)
            Debug.WriteLine("Focused element: {0}", element.ToString());
        else
        {
            Debug.WriteLine("No focused element");
        }
    }
}

So, somewhere in your app, just call WatchWhatsGotFocus.StartWatching() and you'll see what's going on.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号