【问题标题】:How Can I change the way that focus looks like in WPF?如何更改焦点在 WPF 中的外观?
【发布时间】:2011-08-10 16:43:03
【问题描述】:

wpf 在 Windows 7 上提供的焦点视觉提示是一条虚线,如下所示:

现在,我该如何改变它的外观?如何控制它的出现?

谢谢!

【问题讨论】:

  • 继承您使用他们的控制权。当按钮被选中时,重绘按钮你希望它出现的样子。

标签: c# .net wpf focus


【解决方案1】:

试试下面的方法

<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 属性(看到 DynamicResource 的东西?)强>

它还使用动画来改变焦点矩形的颜色。

享受:)

<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>

在这里你看到我有 Button 和 TextBox 的样式,它们为这个窗口中的所有按钮和文本框设置了属性 FocusVisualStyle。

【讨论】:

  • 您的样式仅在键盘焦点而非鼠标焦点上触发
  • 我去看看,我已经有一段时间没有发布它了。我会尽快回来。
  • @user841612,这是因为 FocusVisualStyle 仅在通过键盘获得焦点时使用,而不是通过编程或使用鼠标。
  • 需要一个自定义操作(源自 TriggerAction),但我从来没有机会在家里的机器上工作并编写一些代码。这也超出了这个问题的范围。
猜你喜欢
  • 2013-01-31
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 2011-04-08
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多