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

StackOverflow https://stackoverflow.com/questions/21579517

  •  07-10-2022
  •  | 
  •  

Question

Updated: How can I compare or verify Button.Background to a hexadecimal color such as {#FF008F06} etc.. in Silverlight application using C# code programmatically?

Était-ce utile?

La solution

The Background property is of type Brush, so it can be a SolidColorBrush or a GradientBrush and so on.

I think you need to distinguish between the various types and perform the correct comparison.

For example:

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

To compare a to a specific hexadecimal color:

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

Autres conseils

EDIT Test Assert

var Greenish = new SolidColorBrush(Colors.Green);
Assert.AreEqual(Greenish.Color, ((SolidColorBrush)Button.backGround)).Color;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top