[WPF]为控件中的焦点设置样式以及 FocusVisualStyle

最近的一直在用WPF,也是一边学一边用。客户对页面的要求比较高,原来一直做的Web的项目,这次是C/S结构的项目。

开发时是在Windows7做的,鼠标点击WPF元素时和用Tab使元素获得焦点后的效果不同。使用Tab时会让元素有一个虚框。在Windows XP上两种方式都会使元素有一个虚框。

查了MSDN后,发现有方法设置元素属性(FocusVisualStyle)。

代码
<Page
  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
>
  
<Page.Resources>
    
<Style x:Key="MyFocusVisual">
      
<Setter Property="Control.Template">
        
<Setter.Value>
          
<ControlTemplate>
            
<Rectangle Margin="-2" StrokeThickness="1" Stroke="Red" StrokeDashArray="1 2"/>
          
</ControlTemplate>
        
</Setter.Value>
      
</Setter>
    
</Style>
  
</Page.Resources>
  
<StackPanel Background="Ivory" Orientation="Horizontal">
    
<Canvas Width="10"/>
    
<Button Width="100" Height="30" FocusVisualStyle="{DynamicResource MyFocusVisual}">
      Focus Here
</Button>
    
<Canvas Width="100"/>
    
<Button Width="100" Height="30" FocusVisualStyle="{DynamicResource MyFocusVisual}">
      Focus Here
</Button>
  
</StackPanel>
</Page>

MSDN链接(VS.90)

焦点概述

为控件中的焦点设置样式以及 FocusVisualStyle

FrameworkElement.FocusVisualStyle 属性

FrameworkContentElement.FocusVisualStyle 属性

qishichang

原文地址:https://www.cnblogs.com/qishichang/p/1814035.html