微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何在WPF中改变焦点的方式?

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

您可以根据自己的喜好进行自定义.这只是一个起点.

编辑:由于这么多人喜欢这个解决方案是另一个例子,它改变了所有按钮和文本框的焦点视觉风格,而没有显式设置每个控件的FocusVisualStyle属性(参见DynamicResource thingy?)在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="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的属性.

原文地址:https://www.jb51.cc/csharp/94592.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐