【问题标题】:WPF BackgroundColorWPF 背景颜色
【发布时间】:2011-12-30 15:21:03
【问题描述】:

我想让我的 WPF 应用程序的用户有机会更改应用程序的背景颜色。为此,他有一个包含一些值的组合框:

        cbSetBackground.Items.Add("green");
        cbSetBackground.Items.Add("red");
        cbSetBackground.Items.Add("blue");
        cbSetBackground.Items.Add("yellow");

现在,通过 Click-Event,我必须将背景色设置为所选颜色。我是这样试的

this.Background = Brushes. + cbSetBackground.SelectedItem.ToString();

当然,这行不通。 我该如何管理?

【问题讨论】:

    标签: c# wpf colors


    【解决方案1】:

    您应该能够使用 BrushConverter (http://msdn.microsoft.com/en-us/library/system.windows.media.brushconverter.aspx)。

    BrushConverter conv = new BrushConverter();
    SolidColorBrush brush = conv.ConvertFromString(cbSetBackground.SelectedItem.ToString()) as SolidColorBrush;
    this.Background = brush;
    

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      BrushConverter bc = new BrushConverter();  
      this.Background=  (Brush)bc.ConvertFrom(cbSetBackground.SelectedItem.ToString());
      

      BrushConverter bc = new BrushConverter();  
      this.Background=  (Brush)bc.ConvertFromString(cbSetBackground.SelectedItem.ToString());
      

      Brush myBrush = new SolidBrush(Color.FromName(cbSetBackground.SelectedItem.ToString()));
      this.Background=myBrush;
      

      在此处查看BrushConverter 课程http://msdn.microsoft.com/en-us/library/system.windows.media.brushconverter.convertfromstring.aspx

      【讨论】:

      • 非常感谢。你知道吗,我如何将这个背景色设置到我的整个应用程序中?我必须在构造函数中提交它吗?
      【解决方案3】:

      这应该可以工作

      string colorStr = cbSetBackground.SelectedItem.ToString();
      Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(colorStr);
      
      this.Background = c;
      

      但您可能需要将第一个字符更改为大写。

      【讨论】:

        猜你喜欢
        • 2012-12-18
        • 1970-01-01
        • 2011-11-24
        • 1970-01-01
        • 2011-08-29
        • 2013-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多