从Windows10核心-XAML上的按钮上删除焦点
本文关键字:按钮 删除 焦点 核心 -XAML Windows10 | 更新日期: 2023-09-27 18:21:39
我有一个带有背景图像和白色文本的按钮。当我点击它或将鼠标放在上面(焦点)时,显示一个黑色边框,文本保持黑色。我该怎么做才能忽略鼠标并在按下时呈现点击视觉效果?
已经添加了这个,但没有效果。
<Style x:Key="ButtonActionStyle" TargetType="Button">
...
<Setter Property="UseLayoutRounding" Value="False"/>
<Setter Property="UseSystemFocusVisuals" Value="False"/>
</Style>
谢谢
这个代码做了我需要的,但没有点击视觉效果:x
<Style x:Key="ButtonActionStyle" TargetType="Button">
...
<Setter Property="UseLayoutRounding" Value="False"/>
<Setter Property="UseSystemFocusVisuals" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Row)" Storyboard.TargetName="grid">
<DiscreteObjectKeyFrame KeyTime="1">
<DiscreteObjectKeyFrame.Value>
<x:Int32>1</x:Int32>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="PointerOver"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Grid x:Name="grid" Margin="0" Grid.Row="0" Grid.RowSpan="1">
<Border
BorderBrush="{TemplateBinding Background}"
BorderThickness="0"
CornerRadius="0"
Background="{TemplateBinding Background}"/>
<ContentPresenter>
<TextBlock
FontFamily="{TemplateBinding FontFamily}"
SelectionHighlightColor="{TemplateBinding Foreground}"
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="Auto"
Width="Auto"
Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
</ContentPresenter>
</Grid>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
基于本教程,它已经完成了。
<Style x:Key="ButtonActionStyle" TargetType="Button">
...
<Setter Property="UseLayoutRounding" Value="False"/>
<Setter Property="UseSystemFocusVisuals" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver"/>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Target="grid.(Grid.Row)" Value="0"/>
<Setter Target="grid.(Canvas.ZIndex)" Value="0"/>
<Setter Target="grid.(UIElement.RenderTransformOrigin)">
<Setter.Value>
<Foundation:Point>0.5,0.5</Foundation:Point>
</Setter.Value>
</Setter>
<Setter Target="grid.(UIElement.RenderTransform).(CompositeTransform.ScaleY)" Value="1"/>
<Setter Target="grid.(UIElement.Projection).(PlaneProjection.LocalOffsetX)" Value="5"/>
<Setter Target="grid.(UIElement.Projection).(PlaneProjection.LocalOffsetY)" Value="5"/>
<Setter Target="grid.(UIElement.Projection).(PlaneProjection.LocalOffsetZ)" Value="0"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid>
<Grid x:Name="grid" Margin="0" Grid.Row="0" Grid.RowSpan="1">
<Grid.Projection>
<PlaneProjection/>
</Grid.Projection>
<Grid.RenderTransform>
<CompositeTransform/>
</Grid.RenderTransform>
<Border
BorderBrush="{TemplateBinding Background}"
BorderThickness="0"
CornerRadius="0"
Background="{TemplateBinding Background}"/>
<ContentPresenter>
<TextBlock
FontFamily="{TemplateBinding FontFamily}"
SelectionHighlightColor="{TemplateBinding Foreground}"
FontSize="{TemplateBinding FontSize}"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="Auto"
Width="Auto"
Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
</ContentPresenter>
</Grid>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>