动画 WPF 菜单

发布于 2024-09-11 00:44:57 字数 659 浏览 2 评论 0原文

如果我有类似的内容:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="FIRST"  Margin="5" VerticalAlignment="Center" />
    <TextBlock Text="SECOND" Margin="5" VerticalAlignment="Center" />
    <TextBlock Text="THIRD"  Margin="5" VerticalAlignment="Center" />
    <TextBlock Text="FOURTH" Margin="5" VerticalAlignment="Center" />
    <TextBlock Text="FIFTH"  Margin="5" VerticalAlignment="Center" />
</StackPanel>

您能否向我展示如何创建一个动画,其中 TextBlock 将从屏幕右侧逐个滚动?

你能给我看一个类似的例子吗?

我知道我可能需要使用 Canvas 而不是 StackPanel,但如何正确排列它们我不知道......

If I have something like:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="FIRST"  Margin="5" VerticalAlignment="Center" />
    <TextBlock Text="SECOND" Margin="5" VerticalAlignment="Center" />
    <TextBlock Text="THIRD"  Margin="5" VerticalAlignment="Center" />
    <TextBlock Text="FOURTH" Margin="5" VerticalAlignment="Center" />
    <TextBlock Text="FIFTH"  Margin="5" VerticalAlignment="Center" />
</StackPanel>

Could you please show me how can I create an animation where TextBlocks will roll out from the right side of the screen, each after another?

Could you show me a similar example?

I know that I probably gonna need to use Canvas instead of StackPanel, but how to arrange them properly then I don't know...

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

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

发布评论

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

评论(1

浮生面具三千个 2024-09-18 00:44:57

这是一个有两个示例的答案。如果您确实想在 StackPanel 中使用 TextBlock,我已经展示过。但是,如果您实际上正在寻找菜单,我也包含了该示例。这两个示例实际上是相同的 - 只是获得动画变化的对象。

<DockPanel HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch" 
           ClipToBounds="True">
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
        <TextBlock Text="FIRST"  Margin="5" VerticalAlignment="Center" />
        <TextBlock Text="SECOND" Margin="5" VerticalAlignment="Center" />
        <TextBlock Text="THIRD"  Margin="5" VerticalAlignment="Center" />
        <TextBlock Text="FOURTH" Margin="5" VerticalAlignment="Center" />
        <TextBlock Text="FIFTH"  Margin="5" VerticalAlignment="Center" />
        <StackPanel.RenderTransform>
            <TranslateTransform x:Name="translateTransform"
                                X="{Binding Path=ActualWidth,
                                            RelativeSource={RelativeSource FindAncestor,
                                                                           AncestorType={x:Type DockPanel}}}" />
        </StackPanel.RenderTransform>
        <StackPanel.Triggers>
            <EventTrigger RoutedEvent="StackPanel.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="translateTransform"
                                             Storyboard.TargetProperty="(TranslateTransform.X)"
                                             To="0"
                                             BeginTime="0:0:0.5"
                                             AutoReverse="False"
                                             Duration="0:0:2.5" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </StackPanel.Triggers>
    </StackPanel>

    <Menu DockPanel.Dock="Top">
        <MenuItem Header="First" />
        <MenuItem Header="Second" />
        <MenuItem Header="Third" />
        <MenuItem Header="Fourth" />
        <MenuItem Header="Fifth" />
        <Menu.RenderTransform>
            <TranslateTransform x:Name="translateTransform2"
                                X="{Binding Path=ActualWidth,
                                            RelativeSource={RelativeSource FindAncestor,
                                                                           AncestorType={x:Type DockPanel}}}" />
        </Menu.RenderTransform>
        <Menu.Triggers>
            <EventTrigger RoutedEvent="Menu.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="translateTransform2"
                                             Storyboard.TargetProperty="(TranslateTransform.X)"
                                             To="0"
                                             BeginTime="0:0:3.5"
                                             AutoReverse="False"
                                             Duration="0:0:2.5" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Menu.Triggers>
    </Menu>

    <Grid>
        <TextBlock FontSize="25"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Text="Content Goes Here" />
    </Grid>

</DockPanel>

Here is a two-example answer. If you truly want to use TextBlocks in a StackPanel, I've shown that. However, if you are actually looking for a menu, I've included that example as well. Both examples are really the same - just the object that gets animated changes.

<DockPanel HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch" 
           ClipToBounds="True">
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
        <TextBlock Text="FIRST"  Margin="5" VerticalAlignment="Center" />
        <TextBlock Text="SECOND" Margin="5" VerticalAlignment="Center" />
        <TextBlock Text="THIRD"  Margin="5" VerticalAlignment="Center" />
        <TextBlock Text="FOURTH" Margin="5" VerticalAlignment="Center" />
        <TextBlock Text="FIFTH"  Margin="5" VerticalAlignment="Center" />
        <StackPanel.RenderTransform>
            <TranslateTransform x:Name="translateTransform"
                                X="{Binding Path=ActualWidth,
                                            RelativeSource={RelativeSource FindAncestor,
                                                                           AncestorType={x:Type DockPanel}}}" />
        </StackPanel.RenderTransform>
        <StackPanel.Triggers>
            <EventTrigger RoutedEvent="StackPanel.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="translateTransform"
                                             Storyboard.TargetProperty="(TranslateTransform.X)"
                                             To="0"
                                             BeginTime="0:0:0.5"
                                             AutoReverse="False"
                                             Duration="0:0:2.5" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </StackPanel.Triggers>
    </StackPanel>

    <Menu DockPanel.Dock="Top">
        <MenuItem Header="First" />
        <MenuItem Header="Second" />
        <MenuItem Header="Third" />
        <MenuItem Header="Fourth" />
        <MenuItem Header="Fifth" />
        <Menu.RenderTransform>
            <TranslateTransform x:Name="translateTransform2"
                                X="{Binding Path=ActualWidth,
                                            RelativeSource={RelativeSource FindAncestor,
                                                                           AncestorType={x:Type DockPanel}}}" />
        </Menu.RenderTransform>
        <Menu.Triggers>
            <EventTrigger RoutedEvent="Menu.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="translateTransform2"
                                             Storyboard.TargetProperty="(TranslateTransform.X)"
                                             To="0"
                                             BeginTime="0:0:3.5"
                                             AutoReverse="False"
                                             Duration="0:0:2.5" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Menu.Triggers>
    </Menu>

    <Grid>
        <TextBlock FontSize="25"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Text="Content Goes Here" />
    </Grid>

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