【问题标题】:How to change Textblock button Content When pressed with a Style in a universal App如何在通用应用程序中使用样式按下时更改文本块按钮内容
【发布时间】:2016-02-11 11:54:29
【问题描述】:

我在按钮内容中设置 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>

【问题讨论】:

  • 我不确定,但在你的代码中你有&lt;Setter Property="Foreground" Value="#00a0e3"/&gt;,然后在按下状态下你将前景更改为相同的颜色&lt;DiscreteObjectKeyFrame KeyTime="0" Value="#00a0e3" /&gt; - 这是故意的吗?
  • 感谢 Romasz 先生的回复,但这对我没有帮助,我已经更新了我的帖子先生

标签: c# xaml win-universal-app windows-10-universal


【解决方案1】:

响应更新, 看你想要完全不同的东西。您需要更改其前景的 TextBlock 位于 ContentPresenterButton 控件之外。因此,您无法通过其样式修改前景。您需要将 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

【讨论】:

  • 我有一个问题先生,点击事件和点击事件有什么区别?
  • Click 事件最初是在 Silverlight 中为桌面 Windows 定义的,它只为 Button 控件(以及 HyperlinkBut​​ton 等衍生控件)定义。您可以将 Click 事件视为处理按钮按下的“传统”方式。 Tap 是在 UIElement 类中定义的,它是许多类型控件的父类。您可以在 TextBlock、Image 和许多其他控件中处理 Tap 事件。 Button 也是 UIElement 的子类,因此也可以接收 Tap 事件。 Button 可以同时接收 Tap 和 Click 事件是多余的。
【解决方案2】:

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

与往常一样,如果您认为此答案有用且正确,请将此答案投票为正确答案。如果您愿意,欢迎您提出更多问题。

【讨论】:

  • 感谢先生的回复,但这并没有帮助:(,我已经更新了我的帖子
猜你喜欢
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多