(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。谢谢你。
解决方案
其他提示
您可能需要考虑使用类似的样式触发器:
<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