单击时如何更改按钮的前景色?

时间:2013-11-07 00:14:54

标签: c# xaml windows-phone-8-sdk

我有这样的事情:

                        <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)
    {
    ?
    }

当用户点击按钮时,我想从白色变为浅灰色。 谢谢。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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