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;}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281827.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281827.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281827.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

gradle 轉 maven

1. 預備 1.1. java 環境 驗證 java -version   1.2. gradle 安裝&#xff0c; 參考&#xff0c; 這里列舉下windows下的安裝 b.1 下載包&#xff1a;https://gradle.org/releases b.2 解壓&#xff0c;例如放到D:\soft\gradle\gradle-3.5 b.3 添加到環境變量&#xff0c; 把D…

詳細的DedeCMS(織夢)目錄權限安全設置教程

一、目錄權限根據統計&#xff0c;絕大部分網站的攻擊都在根目錄開始的&#xff0c;因此&#xff0c;欄目目錄不能設置在根目錄。DEDECMS部署完成后&#xff0c;重點目錄設置如下&#xff1a;1&#xff09;將install刪除。2) data、templets、uploads、a或html目錄&#xff0c;…

java證書的安裝_Java 第三方證書安裝

1、進入dos命令行&#xff0c;進入目錄C:\Program Files (x86)\Java\jre6\lib\security&#xff0c;2、導入命令&#xff1a; keytool -import -keystore "C:\Program Files (x86)\Java\jre6\lib\security\cacerts" -storepass changeit -keypass changeit -alias em…

善用Object.defineProperty巧妙找到修改某個變量的準確代碼位置

2019獨角獸企業重金招聘Python工程師標準>>> 我今天的工作又遇到一個難題。前端UI右下角這個按鈕被設置為"禁用(disabled)"狀態。 這個按鈕的可用狀態由屬性enabled控制。我通過調試發現&#xff0c;一旦下圖第88行代碼執行完畢之后&#xff0c;這個按鈕的…

緩沖運動之框架開始一級簡單框架實例

***********************緩沖運動【框架開始】-1.html********************************************* <!DOCTYPE html> <html><head lang"en"><meta charset"UTF-8"><title></title><style type"text/css&qu…

將字符串分割為指定長度的子字符串

將一個字符串從左至右&#xff0c;分割成指定長度的子字符串&#xff08;注&#xff1a;最后一個子字符串的長度不一定等于指定的長度&#xff09; 1 #include <stdio.h>2 #include <string.h>3 4 #define STR_LENGTH 5 //分割之后每個字符串的長度&#xff08;最…

使用 C# 開發的輕量級開源數據庫 LiteDB

你好&#xff0c;這里是 Dotnet 工具箱&#xff0c;定期分享 Dotnet 有趣&#xff0c;實用的工具或組件&#xff0c;希望對您有用&#xff01;簡介 LiteDB 是一個小型、快速、輕量級的 .NET NoSQL 嵌入式數據庫&#xff0c;也就是我們常說的 K/V 數據庫&#xff0c;完全用 C# …

微信小程序仿微信SlideView組件slide-view

微信小程序仿微信SlideView組件。 使用 1、安裝 slide-view 從小程序基礎庫版本 2.2.1 或以上、及開發者工具 1.02.1808300 或以上開始&#xff0c;小程序支持使用 npm 安裝第三方包。 npm install --save miniprogram-slide-view2、在需要使用 slide-view 的頁面 page.json 中…

hibernate 環境搭建測試

對于hibernate的介紹&#xff0c;網絡上一搜一堆&#xff0c;恐怕我寫的也沒前輩總結的好。這個博主總結的十分好,方便大家欣賞 http://blog.csdn.net/liujiahan629629/article/details/21442607 真正要掌握&#xff0c;還得需要自己動手&#xff0c;才能豐衣足食。所需jar包j…

jQuary的相關動畫效果

第一種&#xff1a;該方法隱藏所有 <p> 元素&#xff1a; <html> <head> <script type"text/javascript" src"jquery.js"></script> <script type"text/javascript"> $(document).ready(function(){ $(&quo…

C# WPF 中使用 MahApps.Metro.IconPacks 提供的圖標

概述我們在桌面應用程序開發時經常會用到很多圖標&#xff0c;時常我是在阿里矢量圖庫下載&#xff1a;https://www.iconfont.cn/&#xff0c;然后存放多項目中去引用&#xff0c;不過這樣操作起來有點繁瑣&#xff0c;這節我們介紹一個更加便捷的方式.用法概述Wpf 圖標管理工具…

rails generator

generate 查找順序 rails/generators/initializer/initializer_generator.rb generators/initializer/initializer_generator.rbrails/generators/initializer_generator.rbgenerators/initializer_generator.rblib/templates/rails/helper/helper.rbcustomize your scaffold t…

java多線程同時運行_Java實現的兩個線程同時運行案例

本文實例講述了Java實現的兩個線程同時運行。分享給大家供大家參考&#xff0c;具體如下&#xff1a;/*** 兩個案例同時運行案例* 1:這個兩個線程并不是有規律的運行而是有沒有規律的交替運行*/package com.test3;public class Demo10_3 {/*** param args*/public static void …

通過幾個例子看sed的模式空間與保持空間

SED之所以能以行為單位的編輯或修改文本&#xff0c;其原因在于它使用了兩個空間&#xff1a;一個是活動的“模式空間&#xff08;pattern space&#xff09;”&#xff0c;另一個是起輔助作用的“暫存緩沖區&#xff08;holdingspace&#xff09;這2個空間的使用。 sed編輯器逐…

dotnet 用 SourceGenerator 源代碼生成技術實現中文編程語言

相信有很多伙伴都很喜歡自己造編程語言&#xff0c;在有現代的很多工具鏈的幫助下&#xff0c;實現一門編程語言&#xff0c;似乎已不是一件十分困難的事情。我利用 SourceGenerator 源代碼生成技術實現了一個簡易的中文編程語言&#xff0c;核心原理是將中文編程語言翻譯為 C#…

斷電后supervisor啟動時報錯

當設備意外斷電后&#xff0c;supervisor在系統啟動時報錯&#xff0c;導致程序假死&#xff0c;此問題可以用systemd來替換supervisor。轉載于:https://www.cnblogs.com/gaoyiping/p/6950470.html

Vue單頁面在ios10系統上出現白屏的bug

一個bug 你用Vue做了一個單頁面應用&#xff0c;它在一切設備上都工作正常&#xff0c;但是突然有一天&#xff0c;你的測試和你說&#xff0c;這個網站在iOS 10上跑不起來&#xff0c;怎么辦&#xff1f;于是你打開你電腦上的Chrome瀏覽器&#xff0c;工作正常&#xff1b;打開…

HTTP/2 規格制定完成

IETF HTTP工作者的負責人Mark Nottingham在其博客上宣布HTTP/2規格制定完成&#xff0c;接下來將是分配RFC編號和正式發表。HTTP是Web的核心技術之一&#xff0c;相比HTTP/1&#xff0c;HTTP/2的改進之處包括更快的頁面加載&#xff1b;更長久的連接&#xff1b;服務器推送&…

【習題 6-7 UVA - 804】Petri Net Simulation

【鏈接】 我是鏈接,點我呀:) 【題意】 在這里輸入題意 【題解】 模擬就好 【代碼】 /* 1.Shoud it use long long ? 2.Have you ever test several sample(at least therr) yourself? 3.Can you promise that the solution is right? At least,the main ideal 4.use the p…

easyui combobox java_Easyui的combobox實現動態數據級聯效果

實現從數據庫中動態獲取對應的list集合&#xff0c;并在easyui的combobox中顯示出來。實現的效果如下&#xff1a;1、數據庫的表設計如圖所示2、數據庫中填寫相關的數據&#xff0c;如圖所示。如圖所示【法律法規】是所屬欄目&#xff0c;因此他的字段parentid是0。【中國公民出…