2009-08-03 9 views
20

Próbuję wyłączyć efekt MouseOver na przyciskach lub przynajmniej zmienić jego kolor w WPF.Jak wyłączyć efekty MouseOver na przycisku w WPF?

Używam następujący styl:

<Style x:Key="Borderless" TargetType="{x:Type Button}"> 
      <Setter Property="OverridesDefaultStyle" Value="True"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Button Background="{TemplateBinding Control.Background}" 
           Focusable="False"> 
          <ContentPresenter 
        Margin="{TemplateBinding Control.Padding}" 
        HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
        VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
        SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" 
        ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
        RecognizesAccessKey="True" 
        Content="{TemplateBinding ContentControl.Content}" /> 
          </Button> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

w Window.Resources, co myślałem, że zastępują wszystkie domyślne zachowanie. Ale tak nie jest.

Wszelkie sugestie?

Odpowiedz

37

Look co szablon kontrola sprowadza się do:

<ControlTemplate TargetType="{x:Type Button}"> 
    <Button> 
     <ContentPresenter/> 
    </Button> 
</ControlTemplate> 

Mówisz: „Chcę, aby zastąpić wygląd mojego przycisk z ... przycisk”. Użycie ControlTemplate polega na zastąpieniu drzewa wizualnego kontrolki. Więc zastępujesz wizualne drzewo istniejącego przycisku innym przyciskiem. Jeśli chcesz rozpocząć guzik od podstaw, można użyć przycisku SimpleStyles:

<Style TargetType="{x:Type Button}"> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="OverridesDefaultStyle" Value="true"/> 
    <Setter Property="MinHeight" Value="23"/> 
    <Setter Property="MinWidth" Value="75"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Border Name="Border" CornerRadius="2" BorderThickness="1" 
        Background="#C0C0C0" 
        BorderBrush="#404040"> 
       <ContentPresenter Margin="2" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center" 
           RecognizesAccessKey="True"/> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsKeyboardFocused" Value="true"> 
        <Setter TargetName="Border" 
          Property="BorderBrush" Value="#202020" /> 
       </Trigger> 
       <Trigger Property="IsDefaulted" Value="true"> 
        <Setter TargetName="Border" 
          Property="BorderBrush" Value="#202020" /> 
       </Trigger> 
       <Trigger Property="IsMouseOver" Value="true"> 
        <Setter TargetName="Border" 
          Property="Background" Value="#808080" /> 
       </Trigger> 
       <Trigger Property="IsPressed" Value="true"> 
        <Setter TargetName="Border" 
          Property="Background" Value="#E0E0E0" /> 
        <Setter TargetName="Border" 
          Property="BorderBrush" Value="#606060" /> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="false"> 
        <Setter TargetName="Border" 
          Property="Background" Value="#EEEEEE" /> 
        <Setter TargetName="Border" 
          Property="BorderBrush" Value="#AAAAAA" /> 
        <Setter Property="Foreground" Value="#888888"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

Zauważ, że ten szablon tworzy przycisk najprostszy możliwy sposób: obramowanie, który zawiera treść przycisk. Nie używa innego przycisku osadzonego w szablonie.

+1

To świetnie, Charlie. Teraz rozumiem. Twoje zdrowie. – jarmond

+0

Dziękuję za ten Charlie, pomógł mi ogromnie. – billb

+1

To uratowało mi życie, dzięki. Muszę przejrzeć układ utworzony przez użytkownika za pomocą przezroczystych przycisków, ale efekt zawisu zrujnował moje plany. – mico

Powiązane problemy