private void Button_Click(object sender, RoutedEventArgs e){
Button btn = (Button)sender;
if(/*insert if condition here*/)
{
cntr = 1;
void1();
}
}
I'm currently developing a C# Windows store app. I have a TextBlock that can have a text of either Red, Orange, Yellow, Green, Blue, Indigo or Violet. I also have seven buttons with different background colors. Now, I want to check if the text of my TextBlock matches the backgound color of the Button clicked.
Use a BrushConverter instance to convert the text of the text block into a Brush object, then compare that brush against the button's background.
Some example XAML:
<StackPanel>
<TextBlock x:Name="MyTextBlock" Text="Red" />
<Button Content="Blue" Background="Blue" Click="OnColorButtonClick" />
<Button Content="Red" Background="Red" Click="OnColorButtonClick" />
<Button Content="Green" Background="Green" Click="OnColorButtonClick" />
<Button Content="Yellow" Background="Yellow" Click="OnColorButtonClick" />
</StackPanel>
...and the button handler code (note that all of the buttons in the example use the same click handler):
private void OnColorButtonClick(object sender, RoutedEventArgs e)
{
var converter = new BrushConverter();
var textblockBrush =
converter.ConvertFromString(MyTextBlock.Text) as Brush;
var button = (Button) sender;
if (button.Background == textblockBrush)
{
// text of my TextBlock matches the backgound color of the Button clicked
}
}