日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF遍历当前容器中某种控件的方法

發布時間:2023/12/6 asp.net 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF遍历当前容器中某种控件的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文:WPF遍歷當前容器中某種控件的方法

版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/m0_37591671/article/details/79528845

WPF遍歷當前容器中某種控件的方法

    • WPF遍歷當前容器中某種控件的方法
      • 1.目的:
      • 2.實現思路:

1.目的:

在設計界面的時候遇到了這樣一個問題:一個窗口中有六個按鈕,我希望點擊某一個按鈕的時候,該按鈕能夠高亮顯示,即:更換該按鈕的背景圖片,點擊第二個的時候,第二個高亮顯示,其他按鈕還是顯示為普通按鈕顏色,如圖:

2.實現思路:

2.1 在每一次點擊的時候,遍歷當前容器中所有Button,但是我們這里只需要下面六個,然后根據按鈕的名稱,來依次給按鈕背景圖片賦予相應的路徑,即還原到普通普片的路徑;在給點擊的按鈕背景圖片賦予高亮圖片的路徑。
2.2 還原到普通普片的路徑

//還原到普通普片的路徑public static void BackToUsedPicture(UIElement uIElement){//遍歷當前容器中所有ButtonList<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遍历当前容器中某种控件的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。