WPF 按钮触摸焦点
WPF Button Touch Focus
我的 xaml 文件中有一个 Button
元素(实际上有 18 个)将专门用于触摸屏。在用户测试期间,发现单击时 Button
没有变成所需的绿色,因为焦点样式覆盖了元素上的 Background
属性。
我已将 Focusable
属性 设置为 false,将 FocusVisualStyle
设置为 {x:Null}
,并尝试修改 FocusManager
TouchUp
处理程序。 None 其中有效,我想知道:
如何防止 Button
的焦点样式出现,或者什么是解决此问题的有效方法?
虽然使用 Style
不是首选解决方案,但有关视觉样式的 Microsoft 文档清楚地表明这是正确的方法:
If you want UI changes for any type of focus, whether via mouse, keyboard, or programmatically, then you should not use focus visual styles, and should instead use setters and triggers in styles or templates that are working from the value of general focus properties such as IsFocused or IsFocusWithin.
深入研究一下样式后,真正改变 Button
样式的 Trigger
是 isMouseOver
事件。
这是 仅触控 设备的解决方案。
<Style x:Key="TouchButton" TargetType="{x:Type Button}">
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="ClickMode" Value="Press"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border" CornerRadius="2" BorderThickness="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="Green"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
最后,将样式应用于需要它的 Button
:
<Button Style="{StaticResource TouchButton}" />
我的 xaml 文件中有一个 Button
元素(实际上有 18 个)将专门用于触摸屏。在用户测试期间,发现单击时 Button
没有变成所需的绿色,因为焦点样式覆盖了元素上的 Background
属性。
我已将 Focusable
属性 设置为 false,将 FocusVisualStyle
设置为 {x:Null}
,并尝试修改 FocusManager
TouchUp
处理程序。 None 其中有效,我想知道:
如何防止 Button
的焦点样式出现,或者什么是解决此问题的有效方法?
虽然使用 Style
不是首选解决方案,但有关视觉样式的 Microsoft 文档清楚地表明这是正确的方法:
If you want UI changes for any type of focus, whether via mouse, keyboard, or programmatically, then you should not use focus visual styles, and should instead use setters and triggers in styles or templates that are working from the value of general focus properties such as IsFocused or IsFocusWithin.
深入研究一下样式后,真正改变 Button
样式的 Trigger
是 isMouseOver
事件。
这是 仅触控 设备的解决方案。
<Style x:Key="TouchButton" TargetType="{x:Type Button}">
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="ClickMode" Value="Press"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border" CornerRadius="2" BorderThickness="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="Green"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
最后,将样式应用于需要它的 Button
:
<Button Style="{StaticResource TouchButton}" />