新学wpf有一个动态更换背景颜色的请问如何操作

xiaottyy 2010-11-24 10:33:09
c#中更换背景这个知道,但是wpf下貌似这个就不行了,请问如何从后台根据鼠标事件,自动的更换laber的背景颜色,谢谢了
...全文
354 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunzhi523 2010-11-24
  • 打赏
  • 举报
回复
以后多多交流 哦。。。
bloodish 2010-11-24
  • 打赏
  • 举报
回复
label1.Background = new SolidColorBrush(Color.FromArgb(255, 128, 128, 128));

html的颜色表示(#xxxxxx),可以转成Color,使用下面的方法:


public static Color GetColor(string rgbHex)
{
if(string.IsNullOrEmpty(rgbHex))
return Color.Empty;
if(rgbHex.Length == 8)
return Color.FromArgb(Convert.ToInt32(rgbHex.Substring(0, 2), 16),
Convert.ToInt32(rgbHex.Substring(2, 2), 16),
Convert.ToInt32(rgbHex.Substring(4, 2), 16),
Convert.ToInt32(rgbHex.Substring(6, 2), 16));

return Color.FromArgb(Convert.ToInt32(rgbHex.Substring(0,2),16),
Convert.ToInt32(rgbHex.Substring(2,2),16),
Convert.ToInt32(rgbHex.Substring(4,2),16));
}
xiaottyy 2010-11-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bloodish 的回复:]

应该没有什么区别,只是WPF中,Element的背景是一个笔刷

C# code

label1.MouseEnter += delegate
{
label1.Background = Brushes.Red;
};

label1.MouseLeave += ……
[/Quote]

这个可以用但是我想换成自定义的颜色比如“#UIHID32”这个如何操作,谢谢了
bloodish 2010-11-24
  • 打赏
  • 举报
回复
应该没有什么区别,只是WPF中,Element的背景是一个笔刷


label1.MouseEnter += delegate
{
label1.Background = Brushes.Red;
};

label1.MouseLeave += delegate
{
label1.Background = Brushes.Blue;
};
xiaottyy 2010-11-24
  • 打赏
  • 举报
回复
bloodish
非常感谢你

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧