如何在WPF中获取背景的颜色

时间:2021-05-31 19:39:47

In my small WPF program I want to show the name of the "background color" of the client area on a mouse click in am message box .... How can I do it?

在我的小的WPF程序我想展示的名字“背景颜色”的客户区在我鼠标点击消息框....我怎么做呢?

System : Win7(32-bit)/VS2008

系统:这个主题(32位)/ VS2008中

Thanks.

谢谢。

1 个解决方案

#1


9  

You need to get the Background object of the element you want to get the color for. Do this in your mouse click event like this:

您需要获取想要获取颜色的元素的背景对象。在鼠标点击事件中这样做:

NOTE: You must check for which brush type, basically SolidColorBrush would only really apply as a gradient would not be a simple color.

注意:您必须检查哪一种笔刷类型,基本上纯色笔刷只适用于渐变不是简单的颜色。

EXAMPLE: Brush backgroundColor = LayoutRoot.Background;

例子:画笔背景颜色= LayoutRoot.Background;

        if (backgroundColor is SolidColorBrush)
        {
            string colorValue = ((SolidColorBrush)backgroundColor).Color.ToString();
        }

#1


9  

You need to get the Background object of the element you want to get the color for. Do this in your mouse click event like this:

您需要获取想要获取颜色的元素的背景对象。在鼠标点击事件中这样做:

NOTE: You must check for which brush type, basically SolidColorBrush would only really apply as a gradient would not be a simple color.

注意:您必须检查哪一种笔刷类型,基本上纯色笔刷只适用于渐变不是简单的颜色。

EXAMPLE: Brush backgroundColor = LayoutRoot.Background;

例子:画笔背景颜色= LayoutRoot.Background;

        if (backgroundColor is SolidColorBrush)
        {
            string colorValue = ((SolidColorBrush)backgroundColor).Color.ToString();
        }