原文: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遍历当前容器中某种控件的方法的更多相关文章

  1. WPF 遍历资源字典中的控件

    object obItem=this.FindResource("canvasdt"); if (obItem is System.Windows.DataTemplate) { ...

  2. WPF中TreeView控件SelectedItemChanged方法的MVVM绑定

    问题描述:左侧treeview控件中点击不同类别的节点时,右侧的页面会显示不同的权限.比如对于My Publications,拥有Modify和Delete两种权限,对于My Subscription ...

  3. 在VS2010中ActiveX控件注册方法,使用regsvr32命令

    上一篇小编展示了如何设置VS2010自带的ActiveX控件的容器测试程序,现在为大家演示一下如何注册ActiveX控件. 首先简单了解一下ActiveX控件的知识,ActiveX控件:简单来说,就是 ...

  4. 关于如何获取Google 官方 NavgationView中的控件的方法

    最近在想要寻找一个好集成的SlidingMenu,看上了官方的DrawLayout,简单易集成 然后如果想动态改变DrawLayout头部的HeaderVIew的资源的话需要先获取到控件 方法如下 n ...

  5. wpf怎么使用WindowsFormsHost(即winform控件)

    原文:wpf怎么使用WindowsFormsHost(即winform控件) 使用方法: 1.首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.NET库中的System ...

  6. Git使用总结 Asp.net生命周期与Http协议 托管代码与非托管代码的区别 通过IEnumerable接口遍历数据 依赖注入与控制反转 C#多线程——优先级 AutoFac容器初步 C#特性详解 C#特性详解 WPF 可触摸移动的ScrollViewer控件 .NET(C#)能开发出什么样的APP?盘点那些通过Smobiler开发的移动应用

    一,原理 首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的 ...

  7. WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探

    原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探         最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...

  8. 示例:WPF中Slider控件封装的缓冲播放进度条控件

    原文:示例:WPF中Slider控件封装的缓冲播放进度条控件 一.目的:模仿播放器播放进度条,支持缓冲任务功能 二.进度: 实现类似播放器中带缓存的播放样式(播放区域.缓冲区域.全部区域等样式) 实现 ...

  9. WPF中Ribbon控件的使用

    这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...

随机推荐

  1. vue 中表单 [v-bind:true-value="a" v-bind:false-value="b"] 的用法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. BZOJ 高精度开根 JAVA代码

    晓华所在的工作组正在编写一套高精度科学计算的软件,一些简单的部分如高精度加减法.乘除法早已写完了,现在就剩下晓华所负责的部分:实数的高精度开m次根.因为一个有理数开根之后可能得到一个无理数,所以这项工 ...

  3. DG性能

    网络带宽 根据primary database redo产生的速率,计算传输redo需要的带宽. 出去tcp/ip网络其余30%的开销,计算需要的带宽公式: 需求带宽=((每秒产生redo的速率峰值/ ...

  4. windows10系统window键失灵,没有反应

    今天键盘的的Window键(win键)按了没反应,某度一圈全是它的垃圾营销号文章,没卵用..最后在微软官方社区支持找到解决方案.也建议大家遇到系统问题到微软社区去寻求帮助,毕竟人家是专业. 解决办法 ...

  5. [React] Use the URL as the source of truth in React

    In Single Page Apps we're used to fetch the data on event callbacks. That disables the capacity to u ...

  6. 联合体union用在何处?

    程序设计刚開始学习的人在学习时,总想问:"这个东东有什么用?"于是,在建设有关的教学资源时,也便总从这个角度,试图给出一些案例,这是一个将刚開始学习的人作为教学目标人群的人该干的事 ...

  7. 自己定义控件的onMeasure方法具体解释

    在我们自己定义控件的时候可能你会用到onMeasure方法,以下就具体的给大家介绍一下这种方法: @Override protected void onMeasure(int widthMeasure ...

  8. [分享]ip地址爬取过滤的shell

    http://www.hbbzy.me/分享ip地址爬取过滤的shell #!/bin/base #ip zhi地址匹配 #获取最新的ip地址 #author:haifeng #wget ftp:// ...

  9. thinkphp内置标签简单讲解

    thinkphp内置标签简单讲解 1.volist循环 name 需要遍历的数据 id 类似于foreach中 value offset 截取数据起始位置 length 截取数据的个数 mod 奇偶数 ...

  10. php实现排列组合

    php实现排列组合 一.总结 1.回溯:回溯的函数参数有些生疏了,记录递归的位置(pos或step),还要有东西(vis数组)来记录这个是否已经被访问 2.php全局变量的使用 :外部定义的普通变量, ...