我怎样才能改变这种专注的样子在WPF的方式吗? [英] How Can I change the way that focus looks like in WPF?

查看:196
本文介绍了我怎样才能改变这种专注的样子在WPF的方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重点的视觉暗示WPF提供在Windows 7上是虚线,因为这这样的:

The focus visual hint that wpf provides on Windows 7 is a dashed line, as such this:

现在,我怎么可以改变它的样子?我怎样才能控制其appearence?

Now, how can I change the way it looks? How can I control its appearence?

谢谢!

推荐答案

尝试像以下

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

编辑:既然有这么多人喜欢这个解决方案在这里是另一个从而改变聚焦视觉样式所有按钮和文本框没有FocusVisualStyle属性为每个控件明确设置的例子(见的DynamicResource啄?)在XAML

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

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

Also it uses animation to change the colour of the focus rectangle.

享受:)

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

在这里,你看我是有的样式按钮和文本框里面设置的属性FocusVisualStyle在此窗口中的所有按钮和文本框。

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.

这篇关于我怎样才能改变这种专注的样子在WPF的方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆