簡體   English   中英

WPF在鼠標懸停時旋轉按鈕的內容/背景

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

當鼠標懸停按鈕時,我試圖旋轉按鈕的內容或背景。

不知道這是否是正確的方法,但是我被卡住了:

 <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>

我的按鈕本來就是這樣,它需要旋轉內容(在這種情況下為網格):

         <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>

我嘗試通過一種風格,但也卡住了。 :s

您就快到了-使用原始按鈕,向網格添加一個變形。 從您的解決方案中獲取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