开发者

Set color through color code in c#

开发者 https://www.devze.com 2023-01-04 02:52 出处:网络
I am trying to add color in c# code, with the following color code for example. ListTreeView.Background = new SolidColorBrush(Colors.White);

I am trying to add color in c# code, with the following color code for example.

ListTreeView.Background = new SolidColorBrush(Colors.White);

This is working..but I want to add this color as color code so I am add as

System.Windows.Media

Could someone give me an example with

System开发者_Go百科.Drawing

So what I can do the following:

ListTreeView.Background = ColorTranslator.FromHtml("#FFE7EFF2");

This gives me error ; Any Ideas?


There isn't an easy way to get the colour with alpha included from a hex string in this way.

I think your answer depends on where you're getting the colour and alpha values from.

The RGB colour alone can be parsed from an HTML hex string:

Color colour = ColorTranslator.FromHtml("#E7EFF2");

If you have a separate alpha value you can then apply this (docs):

Color colour = ColorTranslator.FromHtml("#E7EFF2");
Color transparent = Color.FromArgb(128, colour);

Alternatively you may need to parse the string and split it out to convert the hex pairs into integer values.

PS excuse English spelling, but colour should definitely have a 'u' in it :)


ListTreeView isn't a standard Control provided by the framework, so you'll have to consult their documentation. In general, though, you can use System.Drawing.ColorTranslator.FromHtml or System.Drawing.Color.FromArgb. Here's how you'd do it with a TreeView:

TreeView t = ...
t.BackgroundColor = Color.FromArgb(0xff00ff00); // Fully opaque, 100% green.
// or:
t.BackgroundColor = ColorTranslator.FromHtml("green");


I think you could use System.Drawing.ColorTranslator.FromHtml.


Use the Color.FromArgb method.

Btw, shouldn't it be Treeview.BackColor instead?

Grz, Kris.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号