如何在通用应用程序中使用样式按下时更改文本块按钮内容
本文关键字:文本 按钮 应用程序 样式 | 更新日期: 2023-09-27 18:34:15
我在按钮内容中设置文本块颜色时遇到问题,这是我的代码:
<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>
作为对更新的回应,看到你想要完全不同的东西。需要更改其前景的文本块不在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
正如 Romasz 指出的那样,您已将按钮的前景色设置为与按下状态下按钮的前景色相同,即 #00a0e3。我认为在复制颜色十六进制代码时,您可能粘贴错误。在按下状态或初始前景测试中更改颜色。一旦进行了任一更改,您的代码就可以工作。
与往常一样,如果您发现此答案有用且正确,请将此答案投票为正确答案。如果您愿意,欢迎您提出更多问题。