user2962761
user2962761

Reputation: 130

How do I change foreground color of a button when clicked?

I have something like this:

                        <Grid>
                            <StackPanel Margin="100,0,98,0">
                                <Button x:Name="BtProd"
                                   Content="{Binding Produto}"     
                                   FontSize="{StaticResource PhoneFontSizeLarge}"
                                   Foreground="{Binding Color}"
                                   Click="BtProd_Click">
                                </Button>
                            </StackPanel>
                        </Grid>

How do I change the text color of a button in the C# .cs file?

   private void BtProd_Click(object sender, RoutedEventArgs e)
    {
    ?
    }

I'd like to change from white to lightgray when user clicks the button. Thank You.

Upvotes: 5

Views: 7682

Answers (2)

Dweeberly
Dweeberly

Reputation: 4777

You may want to consider using a style trigger like so:

       <Style x:Key="ButtonPressStyle"
               TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsPressed"
                         Value="True">
                    <Setter Property="Foreground">
                        <Setter.Value>
                            Red
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

Then you define your button like so:

                            <Button x:Name="BtProd"
                               Content="{Binding Produto}"     
                               FontSize="{StaticResource PhoneFontSizeLarge}"
                               Foreground="{Binding Color}"
                               Style="{StaticResource ButtonPressStyle}"
                               Click="BtProd_Click">
                            </Button>

Upvotes: 2

user1085665
user1085665

Reputation:

(sender as Button).Foreground = new SolidColorBrush(Colors.LightGray);

Upvotes: 3

Related Questions