控件获取焦点除了用鼠标外,可以通过键盘来获取,比如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. docker+phantomjs+haproxy 搭建phantomjs集群

    目标: 搭建一个远程的phantomjs服务器,提供高可用服务,支持并发. 原料: 1.docker环境.docker-compose环境 2.phantomjs镜像: docker.io/werni ...

  2. Codeforces791A Bear and Big Brother

    A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...

  3. 所有网卡常用信息获取集中展示(CentOS6 &CentOS7)

    查看所有网卡,状态.光电类型.ip.广播地址.掩码 1.命令如下 ( string='|%-3s|%-18s|%-10s|%-10s|%-10s|%-16s|%-16s|%-16s|'; br=&qu ...

  4. XML语法随记

    1.特殊字符的转义 &  ----  & <  ----  < >  ----  > "   ----  " ' ----- &ap ...

  5. ios调用系统界面显示英文

    调用系统相册界面 UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType ...

  6. C#调用haskell遭遇Attempted to read or write protected memory

    1. Haskell的代码如下: 上面的代码中readMarkdown与writeHtmlString是pandoc中的函数,newString的作用是将String转换为IO CString. 2. ...

  7. TwoSum / Three Sum

    Let's begin with a naive method. We first need to sort the array A[n]. And we want to solve the prob ...

  8. Python 读取文件中unicode编码转成中文显示问题

    Python读取文件中的字符串已经是unicode编码,如:\u53eb\u6211,需要转换成中文时有两种方式 1.使用eval: eval("u"+"\'" ...

  9. 简单题(K-D Tree)

    简单题不简单-- 我们把单点加操作改成插入一个权值为增加量的点,将问题转化成询问一个矩阵中所有点的和,用 \(K-D\ Tree\) 维护,时间复杂度 \(O(n\sqrt{n})\) \(Code\ ...

  10. 机器学习基石笔记:16 Three Learning Principles

    三个理论上界: 三个线性模型: 三个关键工具: 三条学习规则: 1.奥卡姆剃刀定律 先从简单模型开始, 训练后出现欠拟合, 再尝试复杂点模型. 2.采样误差 训练.验证.测试数据尽量同分布. 3.数据 ...