当前位置: 首页 > news >正文

WPF 利用视觉树获取指定名称对象、指定类型对象、以及判断是否有验证错误

1.利用视觉树获取指定名称对象

/// <summary>
/// Finds a Child of a given item in the visual tree.
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter.
/// If not matching item can be found,
/// a null parent is being returned.</returns>
public static T GetChildWithName<T>(this DependencyObject parent, string childName)where T : DependencyObject
{// Confirm parent and childName are valid.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);// If the child is not of the request child type childT childType = child as T;if (childType == null){// recursively drill down the treefoundChild = GetChildWithName<T>(child, childName);// If the child is found, break so we do not overwrite the found child.if (foundChild != null) break;}else if (!string.IsNullOrEmpty(childName)){var frameworkElement = child as FrameworkElement;// If the child's name is set for searchif (frameworkElement != null && frameworkElement.Name == childName){// if the child's name is of the request namefoundChild = (T)child;break;}}else{// child element found.foundChild = (T)child;break;}}return foundChild;
}

2.返回指定类型的子对象

比如:在一个Grid内部有很多TextBox/ComboBox ,需要获取这个控件对象。

var children=Grid.GetChildren(new List<Type>(){ typeof(TextBox),typeof(ComboBox)});

/// <summary>
/// 返回指定类型的子对象
/// </summary>
/// <param name="p_element"></param>
/// <param name="includeTypes"></param>
/// <returns></returns>
public static IEnumerable<UIElement> GetChildren(this DependencyObject p_element, List<Type> includeTypes)
{for (int i = 0; i < VisualTreeHelper.GetChildrenCount(p_element); i++){var child = VisualTreeHelper.GetChild(p_element, i) as UIElement;if (child == null){continue;}Type type = child.GetType();if (includeTypes.Contains(type)){yield return child;}else{foreach (var c in child.GetChildren(includeTypes)){yield return c;}}}
}

3.获取UI对象(UserControl)内部是否存在验证错误

string errorMsg=string.Empty;

if(UserControl.GetChildrenValidationHasError<TextBox>(ref errorMsg))

{

        //UserControl 内部的TextBox 有验证错误

        MessageBox.Show(errorMsg);//提示错误消息

}

/// <summary>
/// 获取当前DependencyObject(含特定类型子对象)是否存在Validation错误
/// </summary>
/// <param name="p_element"></param>
/// <param name="errorMsg"></param>
/// <returns></returns>
public static bool GetChildrenValidationHasError<T>(this DependencyObject p_element, ref string errorMsg) where T : UIElement
{var children = GetChildren<T>(p_element);foreach (var child in children){if (CheckDependencyObjectHasError(child, ref errorMsg)){return true;}}return false;
}/// <summary>
/// 获取当前DependencyObject(含所有UI子对象)是否存在Validation错误
/// </summary>
/// <param name="p_element"></param>
/// <param name="errorMsg"></param>
/// <returns></returns>
public static bool GetChildrenValidationHasError(this DependencyObject p_element, List<Type> includeTypes, ref string errorMsg)
{var children = GetChildren(p_element, includeTypes);foreach (var child in children){if (CheckDependencyObjectHasError(child, ref errorMsg)){return true;}}return false;
}/// <summary>
/// 获取当前DependencyObject是否存在Validation错误
/// </summary>
/// <param name="child"></param>
/// <param name="errorMsg"></param>
/// <returns></returns>
public static bool CheckDependencyObjectHasError(this DependencyObject child, ref string errorMsg)
{if (child != null && Validation.GetHasError(child)){var errors = Validation.GetErrors(child);if (errors != null){errorMsg = string.Join("\n\r", errors.Select(m => m.ErrorContent).ToArray());}return true;}return false;
}

http://www.lryc.cn/news/431027.html

相关文章:

  • 了解`re`模块的`split()`, `sub()`, `subn()`方法的作用
  • 机器学习交通流量预测实现方案
  • QNN:基于QNN+example重构之后的yolov8det部署
  • Redis实战宝典:开发规范与最佳实践
  • RPC的实现原理架构
  • OpenXR Monado Hello_xr提交Frame
  • huggingface快速下载模型及其配置
  • 虚幻5|不同骨骼受到不同伤害|小知识(2)
  • 达梦SQL 优化简介
  • 题解:CF1070B Berkomnadzor
  • shell 学习笔记:数组
  • 计算机基础知识复习9.5
  • spark.sql
  • 2024 数学建模高教社杯 国赛(A题)| “板凳龙”舞龙队 | 建模秘籍文章代码思路大全
  • kaggle注册收不到验证码、插件如何下载安装
  • k8s相关技术栈
  • uniapp h5项目页面中使用了iframe导致浏览器返回按键无法使用, 返回不了上一页.
  • 《2024网络安全十大创新方向》
  • 深入解析反射型 XSS 与存储型 XSS:原理、危害与防范
  • 【STM32+HAL库】---- 驱动MAX30102心率血氧传感器
  • InstantX团队新作!基于端到端训练的风格转换模型CSGO
  • Nginx安全性配置
  • k8s单master多node环境搭建-k8s版本低于1.24,容器运行时为docker
  • taro ui 小程序at-calendar日历组件自定义样式+选择范围日历崩溃处理
  • ARM发布新一代高性能处理器N3
  • 基于Pytorch框架的深度学习U2Net网络天空语义精细分割系统源码
  • 50ETF期权和股指期权有什么区别?ETF期权应该怎么做?
  • JS设计模式之“神奇的魔术师” - 简单工厂模式
  • 【河北航空-注册安全分析报告-无验证方式导致安全隐患】
  • 亚信安慧AntDB-T数据库内核之MVCC机制