单击按钮时,我尝试更改按钮的字体颜色。我尝试了很多不同的东西,但都没有。这是我尝试的最新事情,我认为应该是答案,但它不起作用。有人可以帮忙吗? PointerOver工作正常,但点击按钮时Focused没有做任何事情。
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Normal" />
<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"/>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="ContentPresenter.FontWeight" Value="Bold" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Target="ContentPresenter.Foreground" Value="Purple" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter
x:Name="ContentPresenter"
Content="{TemplateBinding Content}"
ContentTransitions="{TemplateBinding ContentTransitions}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
根据评论编辑: 添加说明 - 我在发布此问题之前尝试使用Pressed,但它并没有做我正在寻找的事情。单击后,我希望颜色保持应用,直到单击另一个按钮,然后单击新按钮中的文本将更改颜色,单击上一个按钮将返回默认颜色。按下后,只要您按下鼠标按钮,文本就会恢复为默认状态。
基于编辑的答案建议: 还提供周围的代码,以查看是否在其他地方犯了错误。
<Page.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="UseSystemFocusVisuals" Value="False" />
<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"/>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="ContentPresenter.FontWeight" Value="Bold" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled">
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Target="ContentPresenter.Foreground" Value="Purple" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter x:Name="ContentPresenter"
Content="{TemplateBinding Content}"
ContentTransitions="{TemplateBinding ContentTransitions}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Style="{StaticResource ButtonStyle}" Content="Button" HorizontalAlignment="Left" Margin="10,180,0,0" VerticalAlignment="Top" />
<Button Style="{StaticResource ButtonStyle}" Content="Button" HorizontalAlignment="Left" Margin="100,180,0,0" VerticalAlignment="Top"/>
<Button Style="{StaticResource ButtonStyle}" Content="Button" HorizontalAlignment="Left" Margin="194,180,0,0" VerticalAlignment="Top"/>
</Grid>
答案 0 :(得分:3)
在UWP中,默认情况下会有{em>系统级焦点视觉应用于Button
和许多其他控件。如果您想像自己在Focused
视觉状态中定义的那样管理自己的焦点视觉效果,请尝试设置
<Setter Property="UseSystemFocusVisuals" Value="False" />
以你的风格。
此外,CommonStates
内的自定义焦点视觉状态不,但FocusStates
。因此,您需要在
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Target="ContentPresenter.Foreground" Value="Purple" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
如果您希望保持对点击的关注,您将需要两件事。首先,您需要将AllowFocusOnInteraction
设置为True
。默认情况下,True
上已为Button
。其次,您希望处理PointerFocused
状态以及Focused
状态。因此,请使用以下
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Target="ContentPresenter.Foreground" Value="Purple" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="PointerFocused">
<VisualState.Setters>
<Setter Target="ContentPresenter.Foreground" Value="Purple" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
为什么呢?因为当您点击来自Button
的{{1}} - Pressed
和来自CommonStates
的{{1}}时,会发生两种状态。点击 Tab 键时,通常会触发PointerFocused
状态。
FocusStates