按钮 VisualState 重点不工作
Button VisualState Focused not working
我正在尝试在单击按钮时更改按钮的字体颜色。我尝试了很多不同的方法,但 none 奏效了。这是我尝试过的最新方法,我认为应该是答案,但它不起作用。有人可以帮忙吗? 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>
在 UWP 中,有一个 系统级视觉焦点 默认应用于 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
。默认情况下,Button
上已经是 True
。其次,您想要处理 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>
为什么?因为当您单击 CommonStates
中的 Button
- Pressed
和 FocusStates
中的 PointerFocused
时,两种状态几乎同时发生。 Focused
状态通常在您点击 Tab 键时触发。
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontSize" Value="40" />
<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="FocusContentPresenter.FontWeight" Value="Bold" />
<Setter Target="ContentPresenter.FontWeight" Value="Bold" />
<Setter Target="ContentPresenter.Foreground" Value="Pink" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Target="ContentPresenter.Foreground" Value="Purple" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Target="FocusContentPresenter.Foreground" Value="Purple" />
<Setter Target="FocusContentPresenter.(UIElement.Opacity)" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="PointerFocused">
<VisualState.Setters>
<Setter Target="FocusContentPresenter.Foreground" Value="Purple" />
<Setter Target="FocusContentPresenter.(UIElement.Opacity)" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter x:Name="ContentPresenter" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" ContentTemplate="{TemplateBinding ContentTemplate}" />
<ContentPresenter x:Name="FocusContentPresenter" Opacity="0" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" ContentTemplate="{TemplateBinding ContentTemplate}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我正在尝试在单击按钮时更改按钮的字体颜色。我尝试了很多不同的方法,但 none 奏效了。这是我尝试过的最新方法,我认为应该是答案,但它不起作用。有人可以帮忙吗? 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>
在 UWP 中,有一个 系统级视觉焦点 默认应用于 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
。默认情况下,Button
上已经是 True
。其次,您想要处理 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>
为什么?因为当您单击 CommonStates
中的 Button
- Pressed
和 FocusStates
中的 PointerFocused
时,两种状态几乎同时发生。 Focused
状态通常在您点击 Tab 键时触发。
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontSize" Value="40" />
<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="FocusContentPresenter.FontWeight" Value="Bold" />
<Setter Target="ContentPresenter.FontWeight" Value="Bold" />
<Setter Target="ContentPresenter.Foreground" Value="Pink" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Target="ContentPresenter.Foreground" Value="Purple" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<VisualState.Setters>
<Setter Target="FocusContentPresenter.Foreground" Value="Purple" />
<Setter Target="FocusContentPresenter.(UIElement.Opacity)" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="PointerFocused">
<VisualState.Setters>
<Setter Target="FocusContentPresenter.Foreground" Value="Purple" />
<Setter Target="FocusContentPresenter.(UIElement.Opacity)" Value="1" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter x:Name="ContentPresenter" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" ContentTemplate="{TemplateBinding ContentTemplate}" />
<ContentPresenter x:Name="FocusContentPresenter" Opacity="0" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" ContentTemplate="{TemplateBinding ContentTemplate}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>