2012-02-14 47 views
18

我該怎麼辦cast顏色名稱SolidColorBrush類型?我的意思是這個詞,即「黃色」。Cast Color Name到SolidColorBrush

SolidColorBrush scb = ??? ; // "Yellow" 

謝謝!

+2

我簡單地將[_construct_](http://msdn.microsoft.com/zh-cn/library/ms558629.aspx)作爲Color的'SolidColorBrush'。 – 2012-02-14 12:25:46

回答

29

爲了獲得顏色,使用:

Color col=(Color)ColorConverter.ConvertFromString("Red"); 

然後創建你的畫筆:

Brush brush=new SolidColorBrush(col); 

,或者您可以使用的顏色,枚舉

Brush brush=new SolidColorBrush(Colors.Red); 
+0

這是完美的WPF,但不適用於Windows Phone 8,如何在WP8中做到這一點 – 2015-11-11 09:11:51

+0

也許這有助於:http://stackoverflow.com/questions/25827964/how-to-convert-a-string-into-一個顏色換窗戶電話-C-銳 – HCL 2015-11-11 09:28:26

3
// Yellow is green + red 
SolidColorBrush yellowBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 255, 0)); 
7

你不能鑄造一個到另一個。他們只是不同的概念。畫筆是畫筆,顏色就是顏色。僅僅因爲畫筆以特定顏色「繪畫」,並不意味着你可以互相交換。

但是,您可以創建一個SolidColorBrush具有特定的顏色,例如:

var brush = new SolidColorBrush(Color.Yellow); 
12

如果你已經知道了顏色的名稱,你可以從Brushes直接將刷:

SolidColorBrush scb = Brushes.Yellow; //scb seems a bit redundant at this point... 

在代碼中,你通常應該使用而不是,除非你有一個你不知道的值。