簡體   English   中英

如何使用C#代碼在Silverlight中將Button.Background與十六進制顏色進行比較或驗證?

[英]How can I compare or verify Button.Background to a hexadecimal color in Silverlight using C# code programmatically?

更新 :如何在Silverlight應用程序中以編程方式使用C#代碼將Button.Background與十六進制顏色(如{#FF008F06}等)進行比較或驗證?

Background屬性的類型為Brush ,因此它可以是SolidColorBrushGradientBrush等。

我認為您需要區分各種類型並執行正確的比較。

例如:

if(Button.Background is SolidColorBrush)
{
    bool isRed = ((SolidColorBrush)Button.Background).Color == Colors.Red;
}
else if (Button.Background is GradientBrush)
{
    ...
}

要將a與特定的十六進制顏色進行比較:

bool flag = ((SolidColorBrush)Button.Background).Color == 
                           (Color)ColorConverter.ConvertFromString("#FF008F06");

編輯測試聲明

var Greenish = new SolidColorBrush(Colors.Green);
Assert.AreEqual(Greenish.Color, ((SolidColorBrush)Button.backGround)).Color;

暫無
暫無

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

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