WPF遍历当前容器中某种控件的方法
2018-03-12 16:24
387 查看
WPF遍历当前容器中某种控件的方法
WPF遍历当前容器中某种控件的方法1.目的:
2.实现思路:
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 Bitm b02c apImage(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; }
相关文章推荐
- asp.net 中遍历页面里某种控件(Textbox)、获取当前页面的名称
- 在代码隐藏中遍历当前页的所有控件的方法
- WPF常用方法,事件驱动和控件遍历
- wpf 根据当前控件,查找父窗口,调用父窗口的方法
- wpf中遍历界面控件的方法
- wpf遍历窗体中所有控件方法
- WPF: WrapPanel 容器的数据绑定(动态生成控件、遍历)
- WPF: WrapPanel 容器的数据绑定(动态生成控件、遍历)
- C# 遍历窗体上控件方法
- C# 遍历窗体上控件方法
- c#程序选中指定容器控件下的相同类型控件的方法
- js中会使用到的一种表单遍历验证的方法,访问当前节点的兄弟节点
- WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法
- javascript客户端遍历控件和怎么获取父容器对象
- vector容器的三种遍历方法
- ios 中在容器中移除单个控件的两个方法Subview
- C# 遍历窗体上控件方法
- WPF TreeView递归遍历相关方法
- WPF ComboBox_SelectionChange事件中获取当前文本的方法
- 遍历window窗体中控件的方法.