简体   繁体   English

如何更改焦点在WPF中的显示方式?

[英]How Can I change the way that focus looks like in WPF?

The focus visual hint that wpf provides on Windows 7 is a dashed line, as such this: wpf在Windows 7上提供的焦点视觉提示是一条虚线,如下所示: FocusExample

Now, how can I change the way it looks? 现在,我该如何改变它的外观? How can I control its appearence? 我怎样才能控制它的外观?

Thanks! 谢谢!

Try something like following 尝试以下内容

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

You can customise to suit your liking. 您可以根据自己的喜好进行自定义。 This is just a starting point. 这只是一个起点。

Edit: Since so many people have liked this solution here is another example which changes focus visual style for all buttons and textboxes without explicit setting of FocusVisualStyle property for each control (see that DynamicResource thingy?) in xaml 编辑:由于这么多人都喜欢这个解决方案,这里是另一个例子,它改变了所有按钮和文本框的焦点视觉风格,而没有为xaml中的每个控件显示FocusVisualStyle属性(请参阅DynamicResource thingy?)

Also it uses animation to change the colour of the focus rectangle. 它还使用动画来改变焦点矩形的颜色。

Enjoy :) 请享用 :)

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

Here you see that I have styles for Button and TextBox which set the property FocusVisualStyle for all the buttons and text boxes in this window. 在这里你可以看到我有Button和TextBox的样式,它为这个窗口中的所有按钮和文本框设置了属性FocusVisualStyle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WPF中设置TextBox的焦点 - How can I set the focus of a TextBox in WPF 如何在WPF样式中更改键盘焦点时更改选项卡边框画笔 - How can I change tab border brush on changing key board focus in wpf style 如何在运行时查看winform的外观? - How can I see what a winform looks like at runtime? 独立的WPF应用程序可以创建类似于Flash接口的接口吗? - Can a standalone WPF application create an interface that looks like a Flash interface? 在不使用WPF的情况下,如何使表单看起来像Windows 7样式,或者至少看起来像现代样式 - Without using WPF, how to make forms looks like Windows 7 style or at least looks like more in modern style 如何以编程方式将键盘焦点放在WPF TreeViewItem上? - How can I programatically get keyboard focus on a WPF TreeViewItem? 看起来像Excel的WPF窗口 - WPF window that looks like excel 如何使按钮像拨动开关一样工作,或者使用拨动开关使拨动开关看起来像按钮? - How can I make a button to act like a toggle or maybe using a toggle and make the toggle to looks like a button? 如何在 WPF 中更改 TextBox 的焦点样式? - How to change focus style of TextBox in WPF? 如何在 wpf mvvm 中的视图 model 中写入失去焦点并获得视图的焦点属性? - How can I write lost focus and got focus property of view in view model in wpf mvvm?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM