2011-05-26 104 views
2

有一种方法可以让失去焦点的人在c#表单中不使用每个组件的LostFocus事件?没有使用控制事件的C#失去焦点

我需要一个屏幕键盘。

我需要将最后一次聚焦控制存储到火按键,但我需要在窗口中完成所有操作。

也是主要的项目是WPF,比我有一些组件嵌套的itemsTemplate等等...

+0

你说的是Windows的表单应用程序? – 2011-05-26 11:10:45

+0

是的,抱歉...不是asp和web – Achilleterzo 2011-05-26 11:23:57

回答

1

我终于用这样的:

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; } 
    } 
0

我不认为有什么办法,直到除非你订阅的事件,并跟踪其失去焦点事件已经解雇最后