【问题标题】:C# Lost Focus without using control eventC#失去焦点而不使用控制事件
【发布时间】:2011-07-06 17:19:02
【问题描述】:

有没有一种方法可以在不使用每个组件的 LostFocus 事件的情况下让谁在 c# 表单中失去焦点?

[编辑]

我需要一个屏幕键盘。

我需要存储最后一个焦点控制来触发按键,但我需要对窗口中的所有内容执行此操作。

主项目也是 wpf,比我有一些组件嵌套为 itemsTemplate 等等......

【问题讨论】:

  • 你说的是windows-forms应用程序吗?
  • 是的,抱歉...不是 asp 和 web

标签: c# wpf lostfocus


【解决方案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; }
    }

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-06-12
      • 2012-11-06
      • 2023-03-19
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多