我有这样的东西:

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

其他提示

您可能需要考虑使用类似的样式触发器:

       <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