比较字符串和背景颜色
Comparing string to background color
private void Button_Click(object sender, RoutedEventArgs e){
Button btn = (Button)sender;
if(/*insert if condition here*/)
{
cntr = 1;
void1();
}
}
我目前正在开发 C# Windows 商店应用程序。
我有一个 TextBlock,它可以包含红色、橙色、黄色、绿色、蓝色、靛蓝或紫色的文本。我还有七个具有不同背景颜色的按钮。现在,我想检查我的 TextBlock 的文本是否与单击的按钮的背景颜色相匹配。
尝试 btn.BackColor.ToString() == textblock.Text 进行比较
所以我制作了一个快速程序,看看它是否可行并且确实可行。只需按照说明更改内容即可。在查看这段代码时只做一件事。尝试了解每一行在做什么。
private void btn2Control_Click(object sender, EventArgs e)//This button color is Control
{
if (label1.BackColor == button2.BackColor)//You are going to want to substatut your label name for label1
{
Console.WriteLine("Here");//This was just to make sure the program did match the colors
}
}
private void btn1Yellow_Click(object sender, EventArgs e)//This button color is Yellow
{
if (label1.BackColor == button1.BackColor)
{
Console.WriteLine("Here");
}
}
由于您使用的是 WPF,因此您需要利用以下属性,
- 对于文本块:
TextBlock.Foreground
(documentation)
- 对于按钮事件:
Control.Background
(documentation)
我不打算写逻辑,因为它是你真正想要的,但你需要做的是获取 TextBLock
中应用的当前前景颜色,并将其与单击按钮的 Background
使用上述属性。
ex:IF
比较代码
if(TextBlock.Foreground == btn.Background){
// Color matching
// Do things here
}
private void Button_Click(object sender, RoutedEventArgs e){
Button btn = (Button)sender;
if(button.Background == textBlock.TextBlock)
{
cntr = 1;
void1();
}
}
使用 BrushConverter 实例将文本块的文本转换为 Brush 对象,然后将该画笔与按钮的背景进行比较.
一些例子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>
...和按钮处理程序代码(请注意示例中的所有按钮都使用相同的点击处理程序):
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
}
}
private void Button_Click(object sender, RoutedEventArgs e){
Button btn = (Button)sender;
if(/*insert if condition here*/)
{
cntr = 1;
void1();
}
}
我目前正在开发 C# Windows 商店应用程序。 我有一个 TextBlock,它可以包含红色、橙色、黄色、绿色、蓝色、靛蓝或紫色的文本。我还有七个具有不同背景颜色的按钮。现在,我想检查我的 TextBlock 的文本是否与单击的按钮的背景颜色相匹配。
尝试 btn.BackColor.ToString() == textblock.Text 进行比较
所以我制作了一个快速程序,看看它是否可行并且确实可行。只需按照说明更改内容即可。在查看这段代码时只做一件事。尝试了解每一行在做什么。
private void btn2Control_Click(object sender, EventArgs e)//This button color is Control
{
if (label1.BackColor == button2.BackColor)//You are going to want to substatut your label name for label1
{
Console.WriteLine("Here");//This was just to make sure the program did match the colors
}
}
private void btn1Yellow_Click(object sender, EventArgs e)//This button color is Yellow
{
if (label1.BackColor == button1.BackColor)
{
Console.WriteLine("Here");
}
}
由于您使用的是 WPF,因此您需要利用以下属性,
- 对于文本块:
TextBlock.Foreground
(documentation) - 对于按钮事件:
Control.Background
(documentation)
我不打算写逻辑,因为它是你真正想要的,但你需要做的是获取 TextBLock
中应用的当前前景颜色,并将其与单击按钮的 Background
使用上述属性。
ex:IF
比较代码
if(TextBlock.Foreground == btn.Background){
// Color matching
// Do things here
}
private void Button_Click(object sender, RoutedEventArgs e){
Button btn = (Button)sender;
if(button.Background == textBlock.TextBlock)
{
cntr = 1;
void1();
}
}
使用 BrushConverter 实例将文本块的文本转换为 Brush 对象,然后将该画笔与按钮的背景进行比较.
一些例子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>
...和按钮处理程序代码(请注意示例中的所有按钮都使用相同的点击处理程序):
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
}
}