【问题标题】:Comparing string to background color比较字符串和背景颜色
【发布时间】:2015-04-29 16:36:03
【问题描述】:
private void Button_Click(object sender, RoutedEventArgs e){
    Button btn = (Button)sender;
    if(/*insert if condition here*/)
    {
        cntr = 1;
        void1();
    }
}

我目前正在开发一个 C# Windows 商店应用程序。 我有一个 TextBlock,它可以包含红色、橙色、黄色、绿色、蓝色、靛蓝或紫罗兰色的文本。我也有七个不同背景颜色的按钮。现在,我想检查我的 TextBlock 的文本是否与单击的按钮的背景颜色匹配。

【问题讨论】:

    标签: c# wpf windows store


    【解决方案1】:

    使用 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
        }
    }
    

    【讨论】:

      【解决方案2】:

      尝试 btn.BackColor.ToString() == textblock.Text 进行比较

      【讨论】:

      • BackColor 是 Windows 窗体中按钮的属性,但 OP 询问的是 WPF。
      【解决方案3】:

      所以我做了一个快速的程序来看看这是否可行并且确实可行。只需按照说明进行更改即可。当您查看此代码时,只需做一件事。尝试并了解每条线路在做什么。

          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");
              }
          }
      

      【讨论】:

      • 你如何改变按钮背景的颜色?
      • 所以我已经研究了很长时间了。我似乎找不到解决方案。我可以告诉你一条可以遵循的道路。您首先需要找出您的文本是什么颜色。所以黑色是#FF000000。然后你需要测试按钮的背景颜色。
      【解决方案4】:
        private void Button_Click(object sender, RoutedEventArgs e){
          Button btn = (Button)sender;
          if(button.Background == textBlock.TextBlock)
          {
              cntr = 1;
              void1();
          }
      }
      

      【讨论】:

      • 虽然这个代码块可能会回答这个问题,但如果你能提供一些解释为什么会这样,那将是一个更好的答案。
      • 我假设这里使用的textBlock 应该引用一个 TextBlock 对象实例。 TextBlock 类没有名为“TextBlock”的属性,因此该代码无法编译。即使您的意思是“文本”属性也不起作用,因为您会尝试将 Brush 对象与 string 对象进行比较。
      • 抱歉,输入错误。正确的是:code if(button.Background == textBlock.foreground)
      【解决方案5】:

      由于您使用的是 WPF,因此您需要利用以下属性,

      我不打算写逻辑,因为它是你真正想要的,但你需要做的是获取TextBLock 中应用的当前前景色,并使用上述属性将其与单击按钮的Background 进行比较.

      ex:IF 比较的代码

       if(TextBlock.Foreground == btn.Background){
            // Color matching
            // Do things here
        }
      

      【讨论】:

      • TextBlock 类没有名为“TextBlock”的静态属性。
      • @Steven Rands 一个错字:)
      猜你喜欢
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      相关资源
      最近更新 更多