简体   繁体   English

在C#wp8 VS2012中获取按钮的颜色

[英]Get a color of a button in C# wp8 VS2012

I have this working code: 我有这个工作代码:

private void Knof6_Click(object sender, RoutedEventArgs e)
    {
        int NakljucnaBarva = RandomNumber(1, 4);
        switch (NakljucnaBarva)
        {
            case 1: Knof6.Background = new SolidColorBrush(Colors.Red);
                break;
            case 2: Knof6.Background = new SolidColorBrush(Colors.Green);
                break;
            case 3: Knof6.Background = new SolidColorBrush(Colors.Blue);
                break;

        }
    }

and I want to get current color of a button and play a sound file if it's red. 我想获取按钮的当前颜色,并播放红色的声音文件。 So far I can't get any useful information from documentation or tutorials. 到目前为止,我无法从文档或教程中获得任何有用的信息。 Any hints? 有什么提示吗?

I would recommend that you use the button's Tag member to store your custom data rather than relying re-using existing functionality. 我建议您使用按钮的Tag成员存储您的自定义数据,而不要依赖于重复使用现有功能。

like this: 像这样:

Knof6.Tag = NakljucnaBarva 

The tag member is specifically there to be 标签成员专门存在

an arbitrary object value that can be used to store custom information about this element. 可用于存储有关此元素的自定义信息的任意对象值。

Here is the link to the doc. 这是文档的链接。

http://msdn.microsoft.com/en-us/library/system.windows.controls.button(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/system.windows.controls.button(v=vs.110).aspx

If Knof6_Click is the click event of the button being pressed and you want to check its background colour you can do something like this. 如果Knof6_Click是被按下按钮的click事件,并且您想检查其背景颜色,则可以执行以下操作。

private void Knof6_Click(object sender, RoutedEventArgs e)
{    
    Button button = (Button)sender; 
    if(button.Background == new SolidColorBrush(Colors.Red))
    {
        // Play sound
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM