版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/m0_37591671/article/details/79528845
WPF遍歷當前容器中某種控件的方法
- WPF遍歷當前容器中某種控件的方法
- 1.目的:
- 2.實現思路:
- WPF遍歷當前容器中某種控件的方法
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;}