Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top