简体   繁体   English

C# 在不使用控制事件的情况下失去焦点

[英]C# Lost Focus without using control event

There is a way to get who lost his focus in a c# form without using the LostFocus event each component?有一种方法可以让谁在 c# 表单中失去焦点而不使用每个组件的 LostFocus 事件?

[edit] [编辑]

I need for a On Screen Keyboard.我需要一个屏幕键盘。

I need to store last focussed control to fire keypress, but i need to do it to all in the window.我需要存储最后一个集中控制来触发按键,但我需要在 window 中对所有内容执行此操作。

Also the main project is wpf, than i have some component nested as itemsTemplate and so on...另外主要项目是wpf,比我有一些嵌套为itemsTemplate的组件等等......

I finally used this:我终于用了这个:

    foreach (Control uie in FindInLogicalTreeDown(this, typeof(TextBox))) AssignEvents(uie);

    private static IEnumerable<DependencyObject> FindInLogicalTreeDown(DependencyObject obj, Type type)
    {
        if (obj != null)
        {
            if (obj.GetType() == type) { yield return obj; }
            foreach (object child in LogicalTreeHelper.GetChildren(obj)) 
                if (typeof(DependencyObject).IsAssignableFrom(child.GetType()))
                    foreach (var nobj in FindInLogicalTreeDown((DependencyObject)child, type)) yield return nobj;
        }
        yield break;
    }

    void AssignEvents(Control element)
    {
        element.GotMouseCapture += new MouseEventHandler(Component_GotFocus);
    }

    public Control LastFocus { get; set; }
    public void Component_GotFocus(object sender, RoutedEventArgs e)
    {
        LastFocus = (Control)sender;
        if (LastFocus.GetType() == typeof(TextBox)) { KeyboardVisible = true; }
    }

i don't think there is any way until unless you subscribe events and keep track which lost focus event has fired last除非您订阅事件并跟踪上次触发的丢失焦点事件,否则我认为没有任何办法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM