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

C#在不使用控制事件的情况下失去焦点,c#,wpf,lostfocus,C#,Wpf,Lostfocus,有一种方法可以在不使用LostFocus事件的情况下,以c#形式确定谁失去了焦点 [编辑] 我需要一个屏幕键盘 我需要将最后一个聚焦控件存储到fire按键,但我需要对窗口中的所有控件执行此操作 另外,主项目是wpf,我有一些组件嵌套为itemsTemplate等等…除非订阅事件并跟踪上次触发的失焦事件,否则我认为没有任何方法。我最终使用了以下方法: foreach (Control uie in FindInLogicalTreeDown(this, typeof(TextBox)))

有一种方法可以在不使用LostFocus事件的情况下,以c#形式确定谁失去了焦点

[编辑]

我需要一个屏幕键盘

我需要将最后一个聚焦控件存储到fire按键,但我需要对窗口中的所有控件执行此操作


另外,主项目是wpf,我有一些组件嵌套为itemsTemplate等等…

除非订阅事件并跟踪上次触发的失焦事件,否则我认为没有任何方法。

我最终使用了以下方法:

    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; }
    }
foreach(FindInLogicalTreeDown中的控制uie(这个,typeof(TextBox)))AssignEvents(uie);
私有静态IEnumerable FindInLogicalTreeDown(DependencyObject对象,类型)
{
如果(obj!=null)
{
if(obj.GetType()==type){yield return obj;}
foreach(LogicalTreeHelper.GetChildren(obj)中的对象子对象)
if(typeof(DependencyObject).IsAssignableFrom(child.GetType()))
foreach(FindInLogicalTreeDown((DependencyObject)子对象,类型)中的var nobj)产生返回nobj;
}
屈服断裂;
}
无效事件(控制元素)
{
element.GotMouseCapture+=新的MouseEventHandler(组件_GotFocus);
}
公共控件LastFocus{get;set;}
公共无效组件(对象发送器、路由目标)
{
LastFocus=(控制)发送方;
if(LastFocus.GetType()==typeof(TextBox)){KeyboardVisible=true;}
}

您是在谈论windows窗体应用程序吗?是的,对不起。。。不是asp和web