我在按钮内容中设置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代码和Textblock,当我按下按钮时,我想将文本颜色从Gray更改为#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>
答案 0 :(得分:2)
响应更新,
看到你想要完全不同的东西。需要更改其前景的TextBlock位于ContentPresenter
Button
控件的<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"/>
之外。因此,通过它的风格,您无法修改前景。您需要将Tap事件添加到按钮,然后从后端代码中更改前景色。请参阅以下代码。
在XAML中 -
restaurantTextBlock.Foreground = new SolidColorBrush(Color.FromArgb(255,0,160,227));//mention any color
在C#---
{{1}}
答案 1 :(得分:1)
正如Romasz所指出的那样,在按下状态即#00a0e3时,您将按钮的前景颜色设置为与按钮的前景颜色相同。我认为在复制十六进制代码时,您可能粘贴错了。在“按下”状态或“初始前景”测试中更改颜色。只要进行了任何更改,您的代码就可以正常工作。
与往常一样,如果您觉得这个答案有用且正确,请将此答案作为正确答案投票。如果你愿意,欢迎你再提问。