5

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.

user2962761
  • 130
  • 2
  • 9
  • Change the "Pressed" `VisualState` in the `Button` Style Template accordingly. If I remember correctly WP doesn't support Style Triggers, and there's no reason to set a UI interaction in code behind. – Chris W. Nov 07 '13 at 06:46

2 Answers2

3
(sender as Button).Foreground = new SolidColorBrush(Colors.LightGray);
xmashallax
  • 1,673
  • 1
  • 17
  • 34
2

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>
Dweeberly
  • 4,668
  • 2
  • 22
  • 41