WPF 得到子指定元素方法和得到指定子元素集合方法MvvM得到焦点

本文介绍了一个用于WPF应用程序中查找UI元素的方法,并提供了一种自定义行为来控制焦点移动,使得当按下回车键时可以将焦点转移到下一个可聚焦元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class UIHelper
{
    /// <summary>
    /// 在Visual里找到想要的元素
    /// childName可为空,不为空就按名字找
    /// </summary>
    public static T FindChild<T>(DependencyObject parent, string childName)
       where T : DependencyObject
    {
        if (parent == null) return null;
 
        T foundChild = null;
 
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
 
            T childType = child as T;
            if (childType == null)
            {
                // 住下查要找的元素
                foundChild = FindChild<T>(child, childName);
 
                // 如果找不到就反回
                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                // 看名字是不是一样
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    //如果名字一样返回
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                // 找到相应的元素了就返回
                foundChild = (T)child;
                break;
            }
        }
 
        return foundChild;
    }
 
    /// <summary>
    /// 得到指定元素的集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="depObj"></param>
    /// <returns></returns>
    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)
        where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }
 
                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }
}

  

public class FocusAdvancement
   {
       public static bool GetAdvancesByEnterKey(DependencyObject obj)
       {
           return (bool)obj.GetValue(AdvancesByEnterKeyProperty);
       }
 
       public static void SetAdvancesByEnterKey(DependencyObject obj, bool value)
       {
           obj.SetValue(AdvancesByEnterKeyProperty, value);
       }
 
       public static readonly DependencyProperty AdvancesByEnterKeyProperty =
           DependencyProperty.RegisterAttached("AdvancesByEnterKey", typeof(bool), typeof(FocusAdvancement),
           new UIPropertyMetadata(OnAdvancesByEnterKeyPropertyChanged));
 
       static void OnAdvancesByEnterKeyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       {
           var element = d as UIElement;
           if (element == null) return;
 
           if ((bool)e.NewValue) element.KeyDown += Keydown;
           else element.KeyDown -= Keydown;
       }
 
       static void Keydown(object sender, KeyEventArgs e)
       {
           if (!e.Key.Equals(Key.Enter)) return;
 
           var element = sender as UIElement;
            
           if (element != null) element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
       }
 
   }
xmlns:FOU="clr-namespace:WPFAppVM.AppViews.AppCustomViews"
 
<TextBox FOU:FocusAdvancement.AdvancesByEnterKey="True" Width="120"></TextBox>
<TextBox IsReadOnly="True" FOU:FocusAdvancement.AdvancesByEnterKey="True" Width="120"></TextBox>
<Button Height="30" Width="80" FOU:FocusAdvancement.AdvancesByEnterKey="True">保存</Button>

  本文转自lpxxn博客园博客,原文链接:http://www.cnblogs.com/li-peng/p/3155049.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值