在通用应用程序中使用样式按下时如何更改文本块按钮内容

How to change Textblock button Content When pressed with a Style in a universal App

我在设置按钮内容中的 TextBlock 颜色时遇到问题,这是我的代码:

<Style  x:Key="ButtonStyle"
    TargetType="Button">
    <Setter Property="Background" Value="#e6e6e6" />
    <Setter Property="BorderBrush" Value="#393185" />
    <Setter Property="Foreground" Value="#00a0e3"/>
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="UseSystemFocusVisuals" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid"
                                               Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="transparent" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="transparent" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                               Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="#00a0e3" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>

                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter x:Name="ContentPresenter"
                      BorderBrush="#393185"

                      Foreground="{TemplateBinding Foreground}"
                      Background="{TemplateBinding Background}"
                      BorderThickness="{TemplateBinding BorderThickness}"
                      Content="{TemplateBinding Content}"
                      ContentTransitions="{TemplateBinding ContentTransitions}"
                      ContentTemplate="{TemplateBinding ContentTemplate}"
                      Padding="{TemplateBinding Padding}"
                      Margin="{TemplateBinding Margin}"
                      VerticalAlignment="Center"
                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                      AutomationProperties.AccessibilityView="Raw"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

问题是当我按下按钮时文本块没有改变前景色 你知道我怎样才能更正我的代码吗 感谢帮助

更新:

这是我的 xaml 代码和我想要在按下按钮时将文本颜色从灰色更改为 #00a0e3 的文本块:

<Button Style="{Binding Source={StaticResource ButtonStyle}}">
                                        <Grid Margin="0" x:Name="Grid2" >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="50"  />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Button Grid.Column="0" Click="MenuButton2_Click">
                                            <Image Source="images/3.png" Height="25" Width="25"  x:Name="img2" Margin="0"/>
                                        </Button>
                                        <TextBlock  Text="Restaurant"  Foreground="Gray" x:Name="res3" Grid.Column="1"/>
                                    </Grid>
                                </Button>

正如 Romasz 指出的那样,您已将按钮的前景色设置为与处于按下状态时按钮的前景色相同,即 #00a0e3 。我想在复制颜色十六进制代码时,您可能粘贴错了。在按下状态或初始前景测试中更改颜色。一旦进行了其中一项更改,您的代码就可以正常工作。

一如既往,如果您觉得此答案有帮助且正确,请将此答案投票为正确答案。欢迎大家继续提问

为了回应更新, 看你想要完全不同的东西。您需要更改其前景的 TextBlock 在 Button 控件的 ContentPresenter 之外。因此,您无法通过其样式修改前景。您需要将 Tap 事件添加到按钮,并从后端代码中更改前景色。请看下面的代码。

在XAML--

<Button Grid.Column="0" Tapped="MenuButton2_Tapped">
                                        <Image Source="images/3.png" Height="25" Width="25"  x:Name="img2" Margin="0"/>
                                    </Button>
                                    <TextBlock x:Name=restaurantTextBlock  Text="Restaurant"  Foreground="Gray" x:Name="res3" Grid.Column="1"/>

在 C# 中---

restaurantTextBlock.Foreground = new SolidColorBrush(Color.FromArgb(255,0,160,227));//mention any color