WPF常用方法,事件驱动和控件遍历
//初始化数据,默认选中第一项,事件驱动
RadioButton btn = FuncClass.GetChildObject<RadioButton>(this.stackPanel1, "btnname");
if (btn != null)
{
btn.IsChecked = true;
btn.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent, btn));
}
遍历一类控件或一个控件:
/// <summary>
/// 根据控件名找子控件
/// <para>调用方法:</para>
/// <para>Button btnMax = FuncClass.GetChildObject(Button)(this, "btnMax");</para>
/// </summary>
/// <typeparam name="T">控件类型</typeparam>
/// <param name="obj">父控件</param>
/// <param name="name">要找的子控件名称</param>
/// <returns>返回子控件,若没找到返回 null</returns>
public static T GetChildObject<T>(System.Windows.DependencyObject obj, string name) where T : System.Windows.FrameworkElement
{
System.Windows.DependencyObject child = null;
T grandChild = null; for (int i = ; i <= VisualTreeHelper.GetChildrenCount(obj) - ; i++)
{
child = VisualTreeHelper.GetChild(obj, i); if (child is T && (((T)child).Name == name | string.IsNullOrEmpty(name)))
{
return (T)child;
}
else
{
grandChild = GetChildObject<T>(child, name);
if (grandChild != null)
return grandChild;
}
}
return null;
} /// <summary>
/// 遍历一类控件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static List<T> GetChildObjects<T>(DependencyObject obj) where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>();
for (int i = ; i <= VisualTreeHelper.GetChildrenCount(obj) - ; i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && !string.IsNullOrEmpty(((T)child).Name))
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects<T>(child));
}
return childList;
}
WPF 窗体居中:
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
WPF常用方法,事件驱动和控件遍历的更多相关文章
- offline页面开发常用方法及页面控件验证
offline页面开发常用方法及页面控件验证,对一些CheckBoxList操作进行封装,新人可以直接使用该代码. 1.返回上一页网址 /// <summary> /// Descript ...
- 在WPF中使用WinForm控件方法
1. 首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2. 在要使用WinForm控 ...
- CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
事件的结果 经过多天的思考及忙碌的开发及测试,CYQ.Data 终于在UI上全面支持WPF,至此,CYQ.Data 已经可以方便支持wpf的开发,同时,框架仍保留最低.net framework2.0 ...
- WPF中的image控件的Source赋值
WPF中的Image控件Source的设置 1.XAML中 简单的方式(Source="haha.png"); image控件的Source设置为相对路径后(Source=&quo ...
- WPF Adorner+附加属性 实现控件友好提示
标题太空泛,直接上图 无论是在验证啊,还是提示方面等一些右上角的角标之类的效果,我们会怎么做? 这里介绍一种稍微简单一些的方法,利用附加属性和Adorner来完成. 例如WPF自带的控件上要加这样的效 ...
- WPF后台设置xaml控件的样式System.Windows.Style
WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵 http://3w.suchso.com/projecteac-tual/wpf-zhi ...
- UIStepper步进器 ——事件驱动型控件,(一个+和-按钮的)
- (void)viewDidLoad { [super viewDidLoad]; //步进器 固定的size (94*27), 事件驱动型控件 UIStepper *st ...
- WPF Step By Step 控件介绍
WPF Step By Step 控件介绍 回顾 上一篇,我们主要讨论了WPF的几个重点的基本知识的介绍,本篇,我们将会简单的介绍几个基本控件的简单用法,本文会举几个项目中的具体的例子,结合这些 例子 ...
- 在WPF中调用Winform控件
最近在项目中用到了人脸识别和指纹识别,需要调用外部设备和接口,这里就用到了在WPF中调用Winform控件. 第一步,添加程序集引用.System.Windows.Forms和WindowsForms ...
随机推荐
- App接口中json方式封装通信接口
封装json通信接口的类 <?php class Response{ /** * 按json方式输出通信数据 * @param integer $code状态码 * @param string ...
- Effective Java之避免创建不必要的对象
Effective Java中有很多值得注意的技巧,今天我刚开始看这本书,看到这一章的时候,我发现自己以前并没有理解什么是不必要的对象,所以拿出来跟大家探讨一下,避免以后犯不必要的错误! 首先书中对不 ...
- [LeetCode] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- windows2003安全加固脚本
@echo off title= Windwos/index.html' target='_blank'>Windows Security echo. echo **************** ...
- mysql is marked as crashed and should be repaired错误
1.mysql数据存放路径默认为/var/lib/mysql/目录 2.用myisamchk命令修复数据表,如: myisamchk -c -r talbe.MYI
- SQL Server 2005 发布 订阅 (配置实例[图])(转载)
2.1 发布&订阅 1. 测 试环境: Item 发布机 A 订阅机 B OS Windows 2003 Server Windows 2003 Server S ...
- Hbase can't get locations问题
2016-11-08 22:21:16,875 ERROR [main-SendThread(ht-lxfx-01:24002)] client.ZooKeeperSaslClient: An err ...
- exe4j中"this executable was created with an evaluation错误解决方法
在使用exe4j时,如果您的exe4j没有注册,在运行有exe4j转换的*.jar为*.exe的可执行文件是会提示:"this executable was created with an ...
- (转) 新的开始之Win7、CentOS 6.4 双系统 硬盘安装
http://blog.csdn.net/cnclenovo/article/details/11358447
- 最长不下降子序列 O(nlogn) || 记忆化搜索
#include<stdio.h> ] , temp[] ; int n , top ; int binary_search (int x) { ; int last = top ; in ...