简体   繁体   中英

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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