2016-04-14 48 views
0

我得到了一个代码,当我按下时,颜色变为黄色,当我释放时,问题不会回到原来的颜色,任何解决方案?(WPF App C#)仅在按下事件时更改颜色,并在发布时更改为原始颜色?

<Button Content="0" FontSize="28" FontWeight="Bold" Height="53" HorizontalAlignment="Left" Margin="67,51,0,0" Name="button10" VerticalAlignment="Top" Width="63" Grid.Column="9" Grid.Row="5" Grid.ColumnSpan="3" Grid.RowSpan="2"> 
     <Button.Style> 
      <Style TargetType="Button"> 
       <Style.Triggers> 
        <Trigger Property="IsPressed" Value="True"> 
         <Trigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Yellow"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </Trigger.EnterActions> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Button.Style> 
    </Button> 

回答

1

如果你没有真正做任何动画如何,你可以用故事板免除:

<Style TargetType="Button"> 
    <Style.Triggers> 
     <Trigger Property="IsPressed" Value="True"> 
      <Setter Property="Background" Value="Yellow" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

这将恢复到先前的背景色当条件不再是真实的。但它只适用于现有背景颜色由样式设置器设置 - 如果您在按钮上放置了Background="Purple"属性,则样式无法触摸背景颜色。

0

<Trigger Property="IsPressed" Value="False"> 
        <Trigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="OriginalColor"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </Trigger.EnterActions> 
       </Trigger> 
+0

或者您可以将ColorAnimation AutoReverse设置为true,但当用户保持按钮按下时,这可能会导致不同的行为。 –