Как изменить цвет на переднем плане кнопки при нажатии?

StackOverflow https://stackoverflow.com/questions/19825584

  •  05-07-2022
  •  | 
  •  

Вопрос

У меня есть что -то вроде этого:

                        <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>

Как изменить цвет текста кнопки в файле c# .cs?

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

Я хотел бы перейти с белого на Lightgray, когда пользователь нажимает кнопку. Благодарю вас.

Это было полезно?

Решение

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

Другие советы

Вы можете рассмотреть возможность использования стиля триггера, как 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>

Затем вы определяете свою кнопку как:

                            <Button x:Name="BtProd"
                               Content="{Binding Produto}"     
                               FontSize="{StaticResource PhoneFontSizeLarge}"
                               Foreground="{Binding Color}"
                               Style="{StaticResource ButtonPressStyle}"
                               Click="BtProd_Click">
                            </Button>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top