Maurizio Reginelli
Maurizio Reginelli

Reputation: 3212

First test of a Windows Phone application

I downloaded the Microsoft Visual Studio 2010 Express for Windows Phone and I wrote a simple application to make a first test of the emulator. In this application I have only a button with the property Content binded to a string called ButtonText and with the property Background binded to a SolidColorBrush named FillColor. I handled the Click event with this code:

    void MyButton_Click(object sender, RoutedEventArgs e)
    {
        if (toggle == true)
        {
            ButtonText = "Blue";
            FillColor = new SolidColorBrush(Colors.Blue);
        }
        else
        {
            ButtonText = "Red";
            FillColor = new SolidColorBrush(Colors.Red);
        }
        toggle = !toggle;
    }

Unfortunately this doesn't work. While the Content of the Button changes each time the button is pressed, I cannot say the same for the Background which remains at the same color.
Could you tell me what is wrong? Thank you.

I also post the XAML:

    <Grid x:Name="ContentGrid" Grid.Row="1">
        <Button Name="MyButton" Width="300" Height="300"
                Content="{Binding Path=ButtonText}" 
                Background="{Binding Path=FillColor}" />
    </Grid>

Upvotes: 2

Views: 327

Answers (1)

Chris Koenig
Chris Koenig

Reputation: 2748

The issue is with the use of "new" in the line:

FillColor = new SolidColorBrush(Colors.Blue);

Using the "new" operation breaks the data binding that was previously set up. Try using the following instead:

FillColor.Color = Colors.Blue;

Replace both the changes to Blue and to Red and that should do the trick.

HTH!
Chris

Upvotes: 1

Related Questions