2013-08-15 27 views
6

Tôi đang viết chương trình bằng WPF (C#). Tôi sử dụng một số nút. Nút Anny bắt đầu nhấp nháy sau khi nhấp vào nút đó! Màu sắc của nó mờ dần từ màu nền đến màu xanh (ánh sáng màu xanh). Nó không ngừng nhấp nháy trong khi tôi nhấp vào một nút khác (sau đó nút này bắt đầu nhấp nháy !!!). Nó là một lỗi? có liên quan đến studio trực quan của tôi không?Cách tránh nút flash WPF

Làm cách nào để khắc phục sự cố này?

+2

của các VisualStyle defualt của nút , Windows đã sử dụng điều này trong nhiều năm, nó không mới, nó xảy ra khi nút có tiêu điểm, nếu bạn muốn dừng nút làm nó, bạn có thể ghi đè lên mẫu nút hoặc đơn giản là không lấy nét nút –

Trả lời

2

Trước tiên, bạn có thể thử đặt nút Focusable thành False. Tuy nhiên, đây sẽ là vấn đề nếu bạn sử dụng điều hướng bàn phím. Tuy nhiên tôi đã tìm thấy mã này Window.Resource XAML:

<Window.Resources> 
<Style x:Key="ButtonFocusVisual"> 
    <Setter Property="Control.Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0"> 
    <GradientStop Color="#F3F3F3" Offset="0"/> 
    <GradientStop Color="#EBEBEB" Offset="0.5"/> 
    <GradientStop Color="#DDDDDD" Offset="0.5"/> 
    <GradientStop Color="#CDCDCD" Offset="1"/> 
</LinearGradientBrush> 
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/> 
<Style x:Key="BoringButtonStyle" TargetType="{x:Type Button}"> 
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Padding" Value="1"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true"> 
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
       </Microsoft_Windows_Themes:ButtonChrome> 
       <ControlTemplate.Triggers> 
        <Trigger Property="ToggleButton.IsChecked" Value="true"> 
         <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Foreground" Value="#ADADAD"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

Sau đó, bất cứ khi nào bạn muốn áp dụng các nút tùy chỉnh phong cách, thiết lập các nút Style tới:

<Button Style="{DynamicResource BoringButtonStyle}"/> 
+0

Microsoft_Windows_Themes: ButtonChrom e đưa ra thông báo lỗi. Đó là Micorsoft.Windows.Themes: ButtonChrome. Nó không hoạt động. Nó cho tôi thông báo rằng ButtonChrome không được hỗ trợ trong dự án WPF. –

2

Bạn có thể ghi đè lên Button mẫu để loại bỏ các mặc định Themed(Aero) phong cách

Something như thế này sẽ là một nơi tốt để bắt đầu.

<Style TargetType="{x:Type Button}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Border x:Name="Border" CornerRadius="2" BorderThickness="1" Background="{x:Static SystemColors.ControlBrush}" BorderBrush="{x:Static SystemColors.ControlDarkDarkBrush}"> 
        <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsKeyboardFocused" Value="True"> 
         <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlBrush}" /> 
         <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlDarkDarkBrush}" /> 
        </Trigger> 
        <Trigger Property="IsDefaulted" Value="True"> 
         <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlBrush}" /> 
         <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlDarkDarkBrush}" /> 
        </Trigger> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter TargetName="Border" Property="Background" Value="{x:Static SystemColors.ActiveCaptionBrush}" /> 
         <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlDarkDarkBrush}" /> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="True"> 
         <Setter TargetName="Border" Property="Background" Value="{x:Static SystemColors.ActiveCaptionBrush}" /> 
         <Setter TargetName="Border" Property="BorderBrush" Value="{x:Static SystemColors.ControlDarkDarkBrush}" /> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter TargetName="Border" Property="Background" Value="#EEEEEE" /> 
         <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>