简体   繁体   English

WPF在鼠标悬停时旋转按钮的内容/背景

[英]WPF rotate content/background of button on mouse over

I'm trying to rotate the content or background of my button when the mouse hovers the button. 当鼠标悬停按钮时,我试图旋转按钮的内容或背景。

Not sure if this is the correct way to do it but i'm stuck: 不知道这是否是正确的方法,但是我被卡住了:

 <Button Width="48" Height="48" Grid.Column="1"
                BorderThickness="0">
      <Button.Triggers>
        <EventTrigger RoutedEvent="Button.MouseEnter">
          <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation Storyboard.TargetName="xxx" 
                               Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
                               By="-360" Duration="0:0:4"
                               AutoReverse="False" RepeatBehavior="Forever" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Button.Triggers>
      <Button.Background>
        <VisualBrush>
          <VisualBrush.Visual>
            <Grid x:Name="xxx" RenderTransformOrigin="0.5,0.5" Width="48" Height="48">
              <Rectangle Fill="Blue" Width="48" Height="48" />
              <Rectangle Fill="Green" Width="14" Height="14" />
              <Grid.RenderTransform>
                <RotateTransform />
              </Grid.RenderTransform>
            </Grid>
          </VisualBrush.Visual>
        </VisualBrush>
      </Button.Background>
    </Button>

My button is originally like this, and it needs to rotate the content (Grid in this case): 我的按钮本来就是这样,它需要旋转内容(在这种情况下为网格):

         <Button Width="48" Height="48" Grid.Column="1"
                BorderThickness="0">
            <Grid>
              <Rectangle Fill="Blue" Width="48" Height="48" />
              <Rectangle Fill="Green" Width="14" Height="14" />
            </Grid>
    </Button>

I tried via a style but also stuck. 我尝试通过一种风格,但也卡住了。 :s :s

You were almost there - use your original Button, add a Tranformation to the Grid. 您就快到了-使用原始按钮,向网格添加一个变形。 Take the Eventtrigger from your solution and only add the name of the Grid ("RotationGrid" in my Solution). 从您的解决方案中获取Eventtrigger,仅添加网格的名称(在我的解决方案中为“ RotationGrid”)。

<Button Width="48" Height="48">
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.MouseEnter">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="RotateGrid" 
                       Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
                       By="-360" Duration="0:0:4"
                       AutoReverse="False" RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Button.MouseLeave">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="RotateGrid" 
                       Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
                       By="0" Duration="0:0:4"
                       AutoReverse="False" RepeatBehavior="Forever" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Button.Triggers>
    <Grid x:Name="RotateGrid">
            <Rectangle Fill="Blue" Width="48" Height="48" />
            <Rectangle Fill="Green" Width="14" Height="14" />
            <Grid.RenderTransform>
                <RotateTransform Angle="0" CenterX="24" CenterY="24"></RotateTransform>
            </Grid.RenderTransform>
    </Grid>
</Button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM