簡體   English   中英

更改純色畫筆顏色的最佳方法

[英]best way to change colors of a solidcolorbrush

我的wpf應用程序中有一個綁定到元素背景的畫筆。

當我在構造函數中初始化它時,我會相應地這樣做:

MessageBackground = new SolidColorBrush(Colors.Red);

現在,在代碼中有時需要將背景更改為綠色。 我一直在這樣做:

MessageBackground = new SolidColorBrush(Colors.LightGreen);

但這對我來說似乎是錯誤的。 消息背景本身是System.Windows.Media.Brush類型。 它似乎沒有任何允許動態更改它的屬性,例如Color或Brush。

您將需要將MessageBackground屬性SolidColorBrush轉換為SolidColorBrush才能設置其Color屬性:

var brush = MessageBackground as SolidColorBrush;
if (brush != null)
    brush.Color = Colors.LightGreen;

盡管僅將屬性設置為新的SolidColorBrush並沒有什么好處,但是您當前的方法非常好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM