控件获取焦点除了用鼠标外,可以通过键盘来获取,比如Tab键或者方向键等,需要设置控件获取键盘焦点时的样式,可以通过设置FrameworkElemnt.FocusVisualStyle属性,

因为几乎所有常用的控件都继承了FrameworkElement,所以绝大部分控件都拥有该属性

        // Summary:
// Gets or sets a property that enables customization of appearance, effects,
// or other style characteristics that will apply to this element when it captures
// keyboard focus. This is a dependency property.
//
// Returns:
// The desired style to apply on focus. The default value as declared in the
// dependency property is an empty static System.Windows.Style. However, the
// effective value at run time is often (but not always) a style as supplied
// by theme support for controls.
public Style FocusVisualStyle { get; set; }

huangcongde

通过以上代码可以看到,FocusVisualStyle就是Style类型的属性,所以可以这样:

<Window x:Class="FocusVisualStyleDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<!--将获取焦点的样式设置为红色边框-->
<Style x:Key="newFocusStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="-2" StrokeThickness="" Stroke="Red"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Margin="122.275,36.227,139.525,217.173" />
<Button Margin="122.275,127.2,139.525,126.2" FocusVisualStyle="{DynamicResource newFocusStyle}"/>
<Button Margin="97.919,209.328,106.481,32.872" FocusVisualStyle="{x:Null}"/>
</Grid>
</Window>

huangcongde

而有些控件在获取焦点后,出现黑色虚线的效果,就是因为该属性的原因,如果想去掉这种效果,只需要将FocusVisualStyle设置为{x:Null}即可

默认的获取键盘焦点样式,会出现黑色虚线边框

 自定义样式后的效果

将FocusVisualStyle设置为{x:null}的效果

WPF设置控件获取键盘焦点时的样式FocusVisualStyle的更多相关文章

  1. WPF 设置控件阴影后,引发的Y轴位置变化问题

    原文:WPF 设置控件阴影后,引发的Y轴位置变化问题 背景 最近遇到一个动画执行时,文本位置变化的问题.如下图: 如果你仔细看的话,当星星变小时,文本往下降了几个像素. 貌似有点莫名其妙,因为控件之间 ...

  2. WPF设置控件获得焦点FocusManager

      简单用法如下: 在父类容器中通过附加属性FocusManager.FocusedElement来绑定需要强制获得焦点的控件,用法如下: <Grid FocusManager.FocusedE ...

  3. JQuery 点击控件获取当前坐标时不兼容IE7

    现在要求在点击文本框时,获取文本框的坐标,需要相对文本框的位置来显示信息. 思路就是,绑定文本框的click 事件,一旦有点击就触发,去调用clickevent 函数执行计算. $('#txt_m') ...

  4. IOS开发中设置控件内容对齐方式时容易混淆的几个属性

    IOS开发中四个容易混淆的属性: 1. textAligment : 文字的水平方向的对齐方式 1> 取值 NSTextAlignmentLeft      = 0,    // 左对齐 NST ...

  5. WPF设置控件获得焦点

    1.这个比较有效 this.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() => { Keyboard.Foc ...

  6. Android 动态设置控件获取焦点

    之前写过一篇博客,简单的介绍了Android 隐藏EditText的焦点,之所以要隐藏EditText的焦点,是因为当应用在第一次进入某个Activity时,由于该页面中的EditText获取了焦点, ...

  7. WPF 程序如何跨窗口/跨进程设置控件焦点

    原文:WPF 程序如何跨窗口/跨进程设置控件焦点 WPF 程序提供了 Focus 方法和 TraversalRequest 来在 WPF 焦点范围内转移焦点.但如果 WPF 窗口中嵌入了其他框架的 U ...

  8. [WPF] 让第一个数据验证出错(Validation.HasError)的控件自动获得焦点

    1. 需求 在上一篇文章 <在 ViewModel 中让数据验证出错(Validation.HasError)的控件获得焦点>中介绍了如何让 Validation.HasError 的控件 ...

  9. WPF中通过代码设置控件的坐标

    用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...

随机推荐

  1. Hdu1205 吃糖果 2017-06-29 14:26 24人阅读 评论(0) 收藏

    吃糖果 Problem Description HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另 ...

  2. Hdu1796 How many integers can you find 2017-06-27 15:54 25人阅读 评论(0) 收藏

    How many integers can you find Time Limit : 12000/5000ms (Java/Other)   Memory Limit : 65536/32768K ...

  3. php apache

    php与Apache配置基础知识 1.在 AllowOverride 设置为 None 时,.htaccess 文件将被完全忽略.当此指令设置为All时,所有具有“.htaccess” 作用域的指令都 ...

  4. [置顶] 获取网络数据中的数组显示成ListView的简单流程

    首先说一下  这是我自己的个人笔记,如果想看看,不用看细节,可以看流程. 定义一个线程池 ExecutorService pool = Executors.newFixedThreadPool(15) ...

  5. spring boot mybatis sql打印到控制台

    如何设置spring boot集成 mybatis 然后sql语句打印到控制台,方便调试: 设置方法: 在application.properties文件中添加: logging.level.com. ...

  6. DELPHI微信支付代码

    DELPHI微信支付代码   不管是微信支付还是支付宝支付, 3个最棘手的问题是:1,如何生成签名2,支付请求如何提交3, 如何验证签名 下面就围绕这二个问题来讲. 我使用的是XE3. 先看微信支付: ...

  7. Linux-帮助与基本命令

    1.获取命令帮助 Linux中的命令按可分类shell内嵌命令.外部命令,获取命令帮助信息前需要区分命令类型. 1.1 type - 查看命令类型 [root@VM_0_171_centos ~]# ...

  8. xampp 80端口被占用后这么办??解决了

    modify port XAMPP: Another web server daemon is already running. 看不懂翻译一下 1. Open the file /opt/lampp ...

  9. 软件测试思维导图[ZZ]

    原文链接 全图

  10. Tree with Small Distances(cf1029E)(树形动规)

    You are given an undirected tree consisting of \(n\) vertices. An undirected tree is a connected und ...