WPF遍历当前容器中某种控件的方法
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37591671/article/details/79528845
WPF遍历当前容器中某种控件的方法
1.目的:
在设计界面的时候遇到了这样一个问题:一个窗口中有六个按钮,我希望点击某一个按钮的时候,该按钮能够高亮显示,即:更换该按钮的背景图片,点击第二个的时候,第二个高亮显示,其他按钮还是显示为普通按钮颜色,如图:
2.实现思路:
2.1 在每一次点击的时候,遍历当前容器中所有Button,但是我们这里只需要下面六个,然后根据按钮的名称,来依次给按钮背景图片赋予相应的路径,即还原到普通普片的路径;在给点击的按钮背景图片赋予高亮图片的路径。
2.2 还原到普通普片的路径
//还原到普通普片的路径
public static void BackToUsedPicture(UIElement uIElement)
{
//遍历当前容器中所有Button
List<Button> btnList=FindChirldHelper.FindVisualChild<Button>(uIElement);
foreach (var item in btnList)
{
Image img = new Image();
if (item.Name== "Weather_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/weatherBUTTON.jpg", UriKind.Relative));
}
else if (item.Name == "Temperature_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/temperatureBUTTON.jpg", UriKind.Relative));
}
else if (item.Name == "Vibration_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/virbrationBUTTON.jpg", UriKind.Relative));
}
else if (item.Name == "Stress_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/stressBUTTON.jpg", UriKind.Relative));
}
else if (item.Name == "Deformation_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/DeformationBUTTON.jpg", UriKind.Relative));
}
else if (item.Name == "Pedestria_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/peopleBUTTON.jpg", UriKind.Relative));
}
item.Content = img;
}
}
2.3 寻找当前容器中某种控件的 方法:
public static class FindChirldHelper
{
public static List<T> FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
try
{
List<T> TList = new List<T> { };
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
{
TList.Add((T)child);
List<T> childOfChildren = FindVisualChild<T>(child);
if (childOfChildren != null)
{
TList.AddRange(childOfChildren);
}
}
else
{
List<T> childOfChildren = FindVisualChild<T>(child);
if (childOfChildren != null)
{
TList.AddRange(childOfChildren);
}
}
}
return TList;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
return null;
}
}
}
3.3 上端使用:
private void Weather_btn_Click(object sender, RoutedEventArgs e)
{
ChangeButtonToLight((Button)sender);
}
public void ChangeButtonToLight(Button button)
{
ChangeButtonImage.BackToUsedPicture(this);
Image img = new Image();
if (button.Name == "Weather_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/weatherBUTTONLight.png", UriKind.Relative));
}
else if (button.Name == "Temperature_btn")
{
img.Source = new BitmapImage(new Uri("../../images/MonitorData/temperatureBUTTONLight.png", UriKind.Relative));
}
else if (button.Name == "Vibration_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/virbrationBUTTON.jpg", UriKind.Relative));
}
else if (button.Name == "Stress_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/stressBUTTON.jpg", UriKind.Relative));
}
else if (button.Name == "Deformation_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/DeformationBUTTON.jpg", UriKind.Relative));
}
else if (button.Name == "Pedestria_btn")
{
img.Source = new BitmapImage(new Uri("../../Images/MonitorData/peopleBUTTON.jpg", UriKind.Relative));
}
button.Content = img;
}WPF遍历当前容器中某种控件的方法的更多相关文章
- WPF 遍历资源字典中的控件
object obItem=this.FindResource("canvasdt"); if (obItem is System.Windows.DataTemplate) { ...
- WPF中TreeView控件SelectedItemChanged方法的MVVM绑定
问题描述:左侧treeview控件中点击不同类别的节点时,右侧的页面会显示不同的权限.比如对于My Publications,拥有Modify和Delete两种权限,对于My Subscription ...
- 在VS2010中ActiveX控件注册方法,使用regsvr32命令
上一篇小编展示了如何设置VS2010自带的ActiveX控件的容器测试程序,现在为大家演示一下如何注册ActiveX控件. 首先简单了解一下ActiveX控件的知识,ActiveX控件:简单来说,就是 ...
- 关于如何获取Google 官方 NavgationView中的控件的方法
最近在想要寻找一个好集成的SlidingMenu,看上了官方的DrawLayout,简单易集成 然后如果想动态改变DrawLayout头部的HeaderVIew的资源的话需要先获取到控件 方法如下 n ...
- wpf怎么使用WindowsFormsHost(即winform控件)
原文:wpf怎么使用WindowsFormsHost(即winform控件) 使用方法: 1.首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.NET库中的System ...
- Git使用总结 Asp.net生命周期与Http协议 托管代码与非托管代码的区别 通过IEnumerable接口遍历数据 依赖注入与控制反转 C#多线程——优先级 AutoFac容器初步 C#特性详解 C#特性详解 WPF 可触摸移动的ScrollViewer控件 .NET(C#)能开发出什么样的APP?盘点那些通过Smobiler开发的移动应用
一,原理 首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的 ...
- WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探
原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探 最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...
- 示例:WPF中Slider控件封装的缓冲播放进度条控件
原文:示例:WPF中Slider控件封装的缓冲播放进度条控件 一.目的:模仿播放器播放进度条,支持缓冲任务功能 二.进度: 实现类似播放器中带缓存的播放样式(播放区域.缓冲区域.全部区域等样式) 实现 ...
- WPF中Ribbon控件的使用
这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...
随机推荐
- day 5 集合
# -*- coding: utf_8 _*_# Author:Vi#集合是无序的 list_1 = [1,2,3,2,3,5,7]list_1 = set(list_1)#将列表转变成集合list_ ...
- flask_wtf flask 的 CSRF 源代码初研究
因为要搞一个基于flask的前后端分离的个人网站,所以需要研究下flask的csrf防护原理. 用的扩展是flask_wtf,也算是比较官方的扩展库了. 先上相关源代码: def validate_c ...
- Mysql学习总结(18)——Mysql主从架构的复制原理及配置详解
一.复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重 ...
- P1145 约瑟夫
P1145 约瑟夫 题目描述 n个人站成一圈,从某个人开始数数,每次数到m的人就被杀掉,然后下一个人重新开始数,直到最后只剩一个人.现在有一圈人,k个好人站在一起,k个坏人站在一起.从第一个好人开始数 ...
- [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor
The upload class will be used in the service layer. Notice it has a constructor for file attribute, ...
- 命令行SVN的使用
1.检出svn co http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码svn co svn://路径(目录或文件的全路径 ...
- 关于Webpack详述系列文章 (第二篇)
1.缩小文件搜索范围 1.1.1 include & exclude module:{ rules:[ { test:/\.js$/, use:['babel-loader?cacheDire ...
- JS错误记录 - To-do List
var data = (localStorage.getItem('todolist'))? JSON.parse(localStorage.getItem('todolist')) : { todo ...
- Ubuntu系统简介
1.ubuntu 远程连接 需要开启ssh 服务 sudo apt-get install openssh-server service ssh start|stop|restart 2.查看Linu ...
- 20160218.CCPP体系具体解释(0028天)
程序片段(01):加法.c 内容概要:字符串计算表达式 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <st ...