wpf 使用自定义颜色代码设置面板背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3246611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:55:52  来源:igfitidea点击:

Setting background of panel with custom color code

wpfbrushescolor-codes

提问by Shyju

In WPF,I can set the background of a stack panel using the below code

在 WPF 中,我可以使用以下代码设置堆栈面板的背景

stackPanelFlasher.Background = Brushes.Aqua;

How can I set the color as a hex color code for example #C7DFFC?

例如,如何将颜色设置为十六进制颜色代码#C7DFFC

回答by HCL

BrushConverter bc = new BrushConverter();  
stackPanelFlasher.Background=  (Brush)bc.ConvertFrom("#C7DFFC"); 

Should do the job. If you want to make it waterproof, better would be

应该做这项工作。如果你想让它防水,最好是

BrushConverter bc = new BrushConverter();  
Brush brush=(Brush)bc.ConvertFrom("#C7DFFC"); 
brush.Freeze();
stackPanelFlasher.Background=brush;

needs fewer resources...

需要更少的资源...

回答by Thomas Levesque

stackPanelFlasher.Background = new SolidColorBrush(Color.FromArgb(alpha, red, green, blue));

回答by NetSide

I think this sample helps you for xaml solution;

我认为这个示例可以帮助您解决 xaml 问题;

 <Border.Background>
       <LinearGradientBrush EndPoint="1.204,0.5" StartPoint="0.056,0.5">
           <GradientStop Color="#FFFFFFFF" Offset="0" />
           <GradientStop Color="#FFD4D7DB" Offset="1" />
       </LinearGradientBrush>                     
  </Border.Background>

回答by nPcomp

The following oneliner should work.

以下 oneliner 应该可以工作。

something.Background = (Brush)new BrushConverter().ConvertFrom("#C7DFFC");