C# 如何更改WPF中焦点的外观?
wpf在Windows 7上提供的焦点视觉提示是一条虚线,如下所示: 现在,我该如何改变它的外观?我如何控制它的外观C# 如何更改WPF中焦点的外观?,c#,.net,wpf,focus,C#,.net,Wpf,Focus,wpf在Windows 7上提供的焦点视觉提示是一条虚线,如下所示: 现在,我该如何改变它的外观?我如何控制它的外观 谢谢 试试下面的方法 <Window x:Class="FocusVisualStyle.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
谢谢 试试下面的方法
<Window x:Class="FocusVisualStyle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="MyFocusVisualStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="-2" StrokeThickness="1" Stroke="Red"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel Orientation="Horizontal" Height="24">
<TextBox Width="96"/>
<Button Content="Yes" Width="64" FocusVisualStyle="{DynamicResource MyFocusVisualStyle}"/>
<Button Content="No" Width="64" FocusVisualStyle="{DynamicResource MyFocusVisualStyle}"/>
</StackPanel>
您可以根据自己的喜好进行定制。这只是一个起点
编辑:由于很多人都喜欢这个解决方案,这里的另一个例子是在xaml中更改所有按钮和文本框的焦点视觉样式,而不显式设置每个控件的FocusVisualStyle属性(请参见DynamicSource thingy?)
它还使用动画来更改焦点矩形的颜色
享受:)
在这里,您可以看到我为按钮和文本框设置了样式,这些样式为该窗口中的所有按钮和文本框设置了属性FocusVisualStyle。使用它们的属性继承控件。选择按钮后,重新绘制按钮的显示方式。您的样式仅在键盘焦点上触发,而不是鼠标焦点。我会查看它,我发布它已经有一段时间了。我会尽快回来。@user841612,那是因为只有通过键盘获得焦点时才使用
FocusVisualStyle
,而不是通过编程或使用鼠标。需要自定义操作(源自TriggerAction),但我从来没有机会在家里用我的机器编写代码。这也超出了这个问题的范围。
<Window x:Class="FocusVisualStyle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="MyFocusVisualStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate >
<Rectangle Margin="-2" StrokeThickness="2" RadiusX="2" RadiusY="2" >
<Rectangle.Stroke>
<SolidColorBrush Color="Red" x:Name="RectangleStroke" />
</Rectangle.Stroke>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded" >
<BeginStoryboard>
<Storyboard>
<ColorAnimation From="Red"
To="Orange"
Duration="0:0:0.5"
RepeatBehavior="Forever"
Storyboard.TargetName="RectangleStroke"
Storyboard.TargetProperty="Color"/>
<DoubleAnimation To="3"
Duration="0:0:0.5"
RepeatBehavior="Forever"
Storyboard.TargetProperty="StrokeDashOffset" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisualStyle}" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisualStyle}" />
</Style>
</Window.Resources>
<StackPanel Orientation="Horizontal" Height="24">
<TextBox Width="96"/>
<Button Content="Yes" Width="64" />
<Button Content="No" Width="64" />
</StackPanel>