当项目尺寸增大或缩小时,如何在 XAML 中将项目保持在 Canvas 上的同一点?

发布于 2024-10-09 13:19:38 字数 1113 浏览 6 评论 0原文

我有以下 xaml,它说明了一个尺寸增大和缩小的圆。我希望动画运行时圆的中心点保持在画布上。

<Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" From="10" To="360" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Width"/>
                        <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" From="10" To="360" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Height"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Window.Triggers>
<Canvas Width="640" Height="480">
    <Ellipse  Width="10" Height="10" Stroke="Red" Canvas.Left="80" Canvas.Top="31"/>
    <Ellipse x:Name="GrowMe" Width="10" Height="10" Stroke="Cyan" Canvas.Left="205" Canvas.Top="203"/> 

I have the following xaml that illustrates a circle that grows and shrinks in size. I want my center point of my circle to stay put on my canvas as the animation runs.

<Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" From="10" To="360" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Width"/>
                        <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" From="10" To="360" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Height"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Window.Triggers>
<Canvas Width="640" Height="480">
    <Ellipse  Width="10" Height="10" Stroke="Red" Canvas.Left="80" Canvas.Top="31"/>
    <Ellipse x:Name="GrowMe" Width="10" Height="10" Stroke="Cyan" Canvas.Left="205" Canvas.Top="203"/> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伤痕我心 2024-10-16 13:19:38

下面是一个按钮的示例,它正是执行此操作。请注意 RenderTransformOrigin 属性。

<Button RenderTransformOrigin="0.5,0.5" Command="{Binding ClickCommand}" Cursor="Hand">
    <Button.RenderTransform>
        <ScaleTransform ScaleX="1" ScaleY="1" />
    </Button.RenderTransform>

    <Button.Template>
        <ControlTemplate>
            <Label>
                <Label.Background>
                    <VisualBrush Visual="{Binding Path=Shape, Converter={StaticResource myTestConv}}" />
                </Label.Background>
            </Label>
        </ControlTemplate>
    </Button.Template>
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
                                                 To="1.3" Duration="0:0:0.1" AccelerationRatio="0.5"/>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY"
                                                 To="1.3" Duration="0:0:0.1" AccelerationRatio="0.5"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
                                                 To="1" Duration="0:0:0.5"/>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY"
                                                 To="1" Duration="0:0:0.5"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    <local:TooltipServiceEx.ToolTipEx>
        <local:ToolTipEx
                HideToolTipTimeout="{StaticResource TooltipTimeout}" />
    </local:TooltipServiceEx.ToolTipEx>
    <ToolTipService.ToolTip>
        <ToolTip
                Template="{StaticResource EventTooltipStyle}" />
    </ToolTipService.ToolTip>
</Button>

Here is an example of a button that does exactly this. Note the RenderTransformOrigin property.

<Button RenderTransformOrigin="0.5,0.5" Command="{Binding ClickCommand}" Cursor="Hand">
    <Button.RenderTransform>
        <ScaleTransform ScaleX="1" ScaleY="1" />
    </Button.RenderTransform>

    <Button.Template>
        <ControlTemplate>
            <Label>
                <Label.Background>
                    <VisualBrush Visual="{Binding Path=Shape, Converter={StaticResource myTestConv}}" />
                </Label.Background>
            </Label>
        </ControlTemplate>
    </Button.Template>
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
                                                 To="1.3" Duration="0:0:0.1" AccelerationRatio="0.5"/>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY"
                                                 To="1.3" Duration="0:0:0.1" AccelerationRatio="0.5"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
                                                 To="1" Duration="0:0:0.5"/>
                                <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY"
                                                 To="1" Duration="0:0:0.5"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    <local:TooltipServiceEx.ToolTipEx>
        <local:ToolTipEx
                HideToolTipTimeout="{StaticResource TooltipTimeout}" />
    </local:TooltipServiceEx.ToolTipEx>
    <ToolTipService.ToolTip>
        <ToolTip
                Template="{StaticResource EventTooltipStyle}" />
    </ToolTipService.ToolTip>
</Button>
孤千羽 2024-10-16 13:19:38

您可以轻松地在 RenderTransform 内设置 ScaleTransform 动画并设置 RenderTransformOrigin = "0.5, 0.5"

--编辑--

然后将故事板更改为以下内容:

<Storyboard>
    <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" From="10" To="180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Width"/>
    <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" From="10" To="180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Height"/>
    <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" By="-180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="(Canvas.Left)"/>
    <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" By="-180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="(Canvas.Top)"/>
</Storyboard>

您需要将增长分成两半并将其添加到宽度/高度并从左/顶部中减去它。

You can do it easily animating ScaleTransform inside RenderTransform and set RenderTransformOrigin = "0.5, 0.5".

--EDIT--

Then your change your storyboard to following:

<Storyboard>
    <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" From="10" To="180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Width"/>
    <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" From="10" To="180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="Height"/>
    <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" By="-180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="(Canvas.Left)"/>
    <DoubleAnimation  RepeatBehavior="Forever" AutoReverse="True" By="-180" Duration="0:0:3" Storyboard.TargetName="GrowMe" Storyboard.TargetProperty="(Canvas.Top)"/>
</Storyboard>

You need to devide your growth into half and add it to Width/Height and subtract it from Left/Top.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文