(sender as Button).Foreground = new SolidColorBrush(Colors.LightGray);
Как изменить цвет на переднем плане кнопки при нажатии?
-
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, когда пользователь нажимает кнопку. Благодарю вас.
Решение
Другие советы
Вы можете рассмотреть возможность использования стиля триггера, как 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>
Не связан с StackOverflow