2016-09-18 1 views
0

Meine WPF-Anwendung aktualisiert nicht Schaltflächenhintergrund für wenn Schaltfläche geklickt UND Mauszeiger ist darüber. Die Hintergrundfarbe ist immer noch hellgrün. Andere Eigenschaftenänderungen funktionieren ok. Was ist falsch?Schaltfläche Hintergrundfarbe wird nicht aktualisiert, wenn die Schaltfläche geklickt

Datei ControlStyles.xaml

... 
<Style TargetType="{x:Type Button}" x:Key="MyButton"> 
     <Setter Property="Foreground" Value="Black"/> 
     <Setter Property="Background" Value="DarkCyan"/>  
     <Setter Property="Margin" Value="5"/> 
     <Setter Property="FontSize" Value="20"/> 
     <Style.Triggers> 
      <Trigger Property="Control.IsMouseOver" Value="true"> 
       <Setter Property="Control.FontStyle" Value="Italic"></Setter> 
       <Setter Property="Control.Foreground" Value="Red"></Setter> 
       <Setter Property="Control.Background" Value="black"></Setter> 
      </Trigger> 
      <Trigger Property="Button.IsPressed" Value="true"> 
       <Setter Property="Control.Foreground" Value="Firebrick"></Setter> 
       <Setter Property="Control.Background" Value="Yellow"></Setter> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
... 

Window.xaml

... 
<Button Name="btn7" Content="7" Grid.Column="0" Grid.Row="3" Style="{StaticResource MyButton}" Click="btn7_Click"/> 
     <Button Name="btn8" Content="8" Grid.Column="1" Grid.Row="3" Style="{StaticResource MyButton}" Click="btn8_Click"/> 
... 

Antwort

0

Sehen Sie, ob das Ihr Problem löst:

<Style TargetType="{x:Type Button}" x:Key="MyButton"> 

    <Setter Property="Foreground" Value="Black"/> 
    <Setter Property="Background" Value="DarkCyan"/>    
    <Setter Property="Margin" Value="5"/> 
    <Setter Property="FontSize" Value="20"/> 

    <Style.Triggers> 
     <Trigger Property="Control.IsMouseOver" Value="true"> 
      <Setter Property="Control.FontStyle" Value="Italic"></Setter> 
      <Trigger.EnterActions> 
       <BeginStoryboard> 
        <Storyboard> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Black"/> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Foreground).(SolidColorBrush.Color)" To="Red"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </Trigger.EnterActions> 
      <Trigger.ExitActions> 
       <BeginStoryboard> 
        <Storyboard> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="DarkCyan"/> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Foreground).(SolidColorBrush.Color)" To="Black"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </Trigger.ExitActions> 
     </Trigger> 
     <Trigger Property="Button.IsPressed" Value="true"> 
      <Trigger.EnterActions> 
       <BeginStoryboard> 
        <Storyboard> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Yellow"/> 
         <ColorAnimation Storyboard.TargetProperty="(Button.Foreground).(SolidColorBrush.Color)" To="Firebrick"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </Trigger.EnterActions>      
     </Trigger>     
    </Style.Triggers> 

</Style> 
+0

Maus über: ist gar nicht funktioniert (Hintergrundfarbe ändert sich nicht). Button-Klick: funktioniert auf seltsame Weise: Es ändert die Farbe, wenn der Mauszeiger von der Schaltfläche entfernt ist –

0

Dies ist meine Lösung mit der Verwendung von Control. Es ist wichtig, dass x: Name und TargetName darin enthalten sind.

ControlTemplate.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <ControlTemplate TargetType="{x:Type Button}" x:Key="MyButtonTemplate"> 
     <Border x:Name="tempBorder" CornerRadius="20" Margin="4" BorderThickness="1" BorderBrush="Black" Background="Gold"> 
      <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" /> 
     </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter TargetName="tempBorder" Property="Background" Value="MediumVioletRed"/> 
      </Trigger> 
      <Trigger Property="IsPressed" Value="True"> 
       <Setter TargetName="tempBorder" Property="Background" Value="LightPink"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 

</ResourceDictionary> 

ConstrolStyles.xaml:

... 
    <Style TargetType="{x:Type Button}" x:Key="MyButton"> 
<!- 
     <Setter Property="Foreground" Value="Black"/> 
     <Setter Property="Background" Value="DarkCyan"/> 

-> ...

Window.xaml:

... 
<Button Name="btnAdd" Content="Add" Grid.Column="4" Grid.Row="1" Style="{StaticResource MyButton}" Template="{StaticResource MyButtonTemplate}" /> 
... 

App.xampl:

<Application x:Class="WpfApplication1.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      > 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Styles/MyColors.xaml"/> 
       <ResourceDictionary Source="Styles/ControlStyles.xaml"/> 
       <ResourceDictionary Source="Styles/ControlTemplates.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 
Verwandte Themen