2011-08-09 36 views
9

Các gợi ý trực quan tập trung mà WPF cung cấp trên Windows 7 là một nét đứt, như vậy này: FocusExampleLàm thế nào tôi có thể thay đổi cách mà tiêu điểm trông giống như trong WPF?

Bây giờ, làm thế nào tôi có thể thay đổi hình thức của nó? Làm thế nào tôi có thể kiểm soát sự xuất hiện của nó?

Cảm ơn!

+0

Thừa kế quyền kiểm soát bạn sử dụng. Khi nút được chọn, hãy vẽ lại nút bạn muốn nó xuất hiện như thế nào. –

Trả lời

22

Hãy thử một cái gì đó như sau

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

Bạn có thể tùy chỉnh cho phù hợp với ý thích của bạn. Đây chỉ là điểm khởi đầu.

Edit: Vì có quá nhiều người thích giải pháp này ở đây là một ví dụ khác mà thay đổi phong cách hình ảnh tập trung cho tất cả các nút và các hộp thoại mà không cần cài đặt rõ ràng tài sản FocusVisualStyle cho mỗi điều khiển (xem rằng DynamicResource thingy?) Trong XAML

Ngoài ra, nó còn sử dụng hoạt ảnh để thay đổi màu của hình chữ nhật tiêu điểm.

Enjoy :)

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

Ở đây bạn thấy rằng tôi có phong cách cho Button và TextBox đó thiết lập các FocusVisualStyle tài sản cho tất cả các nút và hộp văn bản trong cửa sổ này.

+0

Kiểu của bạn chỉ được kích hoạt trên tiêu điểm bàn phím nhưng không lấy tiêu điểm chuột – WPFKK

+0

Tôi sẽ kiểm tra nó, đã khá lâu kể từ khi tôi đăng nó lên. Sẽ quay lại ngay khi có thể. –

+0

@ user841612, đó là vì 'FocusVisualStyle' chỉ được sử dụng khi lấy nét bằng bàn phím, không lập trình hoặc sử dụng chuột. – jnovo

Các vấn đề liên quan