網(wǎng)站首頁 編程語言 正文
一、問題開始
現(xiàn)在有個(gè)需求:
初始狀態(tài)(未選中)的時(shí)候,CheckBox的Content 為 “乒乓球”,然后選中之后,將“乒乓球”就改為“我愛乒乓球” 并且將文字加粗變?yōu)榧t色。
然后就編寫代碼如下:
<Window.Resources> <Style x:Key="cb" TargetType="{x:Type CheckBox}"> <Setter Property="Foreground" Value="Green"></Setter> <Setter Property="FontSize" Value="20"></Setter> <Style.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter Property="Content" Value="我愛乒乓球"></Setter> <Setter Property="FontWeight" Value="Bold"></Setter> <Setter Property="Foreground" Value="Red"></Setter> </Trigger> </Style.Triggers> </Style> </Window.Resources> <WrapPanel VerticalAlignment="Top" Background="LightBlue"> <CheckBox Content="乒乓球" Style="{StaticResource cb}" Margin="10"></CheckBox> </WrapPanel>
實(shí)現(xiàn)效果如下:
奇怪了,為什么文字沒有改變呢?
二、問題說明
以上問題就是使用觸發(fā)器初期很容易犯的錯誤:沒有注意樣式設(shè)置的優(yōu)先級。
如上案例中:<CheckBox Content="乒乓球" Style="{StaticResource cb}" Margin="10"></CheckBox>
將CheckBox自身的元素標(biāo)簽上設(shè)置了Content,這里設(shè)置的屬性具有最高的優(yōu)先級,那么元素標(biāo)簽就不會再去使用其他地方設(shè)置的屬性值,因此無論其他地方如何改變都不會生效。
三、問題訂正
解決該問題只需要將需要在觸發(fā)器中需要設(shè)置的屬性中,將默認(rèn)值設(shè)置到樣式內(nèi),而不是設(shè)置在標(biāo)簽元素自身上。代碼如下所示:
<Window.Resources> <Style x:Key="cb" TargetType="{x:Type CheckBox}"> <Setter Property="Foreground" Value="Green"></Setter> <Setter Property="FontSize" Value="20"></Setter> <Setter Property="Content" Value="乒乓球"></Setter> <Style.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter Property="Content" Value="我愛乒乓球"></Setter> <Setter Property="FontWeight" Value="Bold"></Setter> <Setter Property="Foreground" Value="Red"></Setter> </Trigger> </Style.Triggers> </Style> </Window.Resources> <WrapPanel VerticalAlignment="Top" Background="LightBlue"> <CheckBox Style="{StaticResource cb}" Margin="10"></CheckBox> </WrapPanel>
總結(jié)
原文鏈接:https://blog.csdn.net/qq_39847278/article/details/128512228
相關(guān)推薦
- 2022-03-28 Go實(shí)現(xiàn)用戶每日限額的方法(例一天只能領(lǐng)三次福利)_Golang
- 2022-09-10 Python?turtle庫(繪制螺旋正方形)_python
- 2022-06-12 C語言?詳細(xì)解析時(shí)間復(fù)雜度與空間復(fù)雜度_C 語言
- 2022-05-26 ASP.NET?Core依賴注入詳解_實(shí)用技巧
- 2022-04-27 Shell獲取路徑操作(dirname?$0?pwd)的實(shí)現(xiàn)_linux shell
- 2022-04-29 Go語言線程安全之互斥鎖與讀寫鎖_Golang
- 2022-12-10 C++實(shí)現(xiàn)保存數(shù)據(jù)至EXCEL_C 語言
- 2022-03-27 pygame實(shí)現(xiàn)貪吃蛇游戲_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支