简体   繁体   English

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

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

I have a problem in setting the TextBlock color in my button content,this is my code: 我在按钮内容中设置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>

the problem is that the textblock does not change the Foreground color when I press the button have you please any idea how can I correct my code thanks for help 问题是,当我按下按钮时,文本块不会更改前景颜色。请您知道如何纠正我的代码谢谢您的帮助

Update: 更新:

this is my xaml code and the Textblock that I want to change the text color from Gray to #00a0e3 when I press the button: 这是我的xaml代码和要在按下按钮时将文本颜色从Gray更改为#00a0e3的Textblock:

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

In Response to the Update, see you want completely different thing. 在对更新的响应中,看到您想要完全不同的东西。 The TextBlock whose foreground you need to change, is outside the ContentPresenter of Button control. 需要更改其前景的TextBlock在Button控件的ContentPresenter之外。 Hence, through its style you cannot modify the Foreground. 因此,无法通过其样式来修改前景。 You need to add Tap event to the button and from backend code, you can change the foreground color. 您需要将Tap事件添加到按钮,然后从后端代码中更改前景色。 See the below code. 请参见下面的代码。

In XAML -- 在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"/>

In C#--- 在C#中-

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

As Romasz pointed out, you have set the Foreground color of button same as the foreground color of button when in pressed state ie #00a0e3 . 正如Romasz指出的那样,您已将按钮的前景色设置为与处于按下状态时的按钮前景色相同,即#00a0e3。 I think while copying the color hexadecimal code, you may have pasted wrong. 我认为在复制颜色十六进制代码时,您可能粘贴了错误的代码。 Change the color in either Pressed state or initial Foreground test. 在“已按下”状态或初始“前景”测试中更改颜色。 Your code works though once either of the change is made. 尽管进行了任何更改,您的代码仍然可以工作。

As always, if you find this answer helpful and correct, please vote this answer as correct answer. 与往常一样,如果您认为此答案有帮助且正确,请将该答案投票为正确答案。 You are welcome to ask anymore questions if you like. 如果您愿意,欢迎您再问一些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM