【问题标题】:Begin a StoryBoard when IsOpen property of a Popup is set to True当 Popup 的 IsOpen 属性设置为 True 时开始 StoryBoard
【发布时间】:2011-09-03 00:45:52
【问题描述】:

如何在 Popup 的 IsOpen 属性设置为 True 时开始 StoryBoard?

例如:

<EventTrigger RoutedEvent="{Binding IsOpen, ElementName=pop}">
    <BeginStoryboard>
        <Storyboard>
            <DoubleAnimation Storyboard.TargetName="pop"
                             Storyboard.TargetProperty="Height"
                             Duration="0:0:1"
                             From="0.0"
                             To="200" />
            <DoubleAnimation Storyboard.TargetName="pop"
                             Storyboard.TargetProperty="Width"
                             Duration="0:0:1"
                             From="0.0"
                             To="{Binding ElementName=root,Path=ActualWidth}" />
        </Storyboard>
    </BeginStoryboard>
</EventTrigger>

我知道EventTrigger RoutedEvent="{Binding IsOpen, ElementName=pop}不行

谢谢!

【问题讨论】:

  • 请停止使用 Visual-Studio 标记所有内容,问题与此无关。

标签: wpf xaml animation triggers storyboard


【解决方案1】:

由于您没有标记答案,我认为您仍然需要一些帮助。 这是一个可以按照您的方式工作的代码 sn-p(根据 H.B. 的帖子)

<Window x:Class="WpfTestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="PopupStyle" TargetType="{x:Type Popup}">
        <Style.Triggers>
            <Trigger Property="IsOpen" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation 
                                     Storyboard.TargetProperty="Height"
                                     Duration="0:0:1"
                                     From="0.0"
                                     To="200" />
                            <DoubleAnimation 
                                     Storyboard.TargetProperty="Width"
                                     Duration="0:0:1"
                                     From="0.0"
                                     To="500" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers> 

    </Style>
</Window.Resources>
<Grid>
    <Button Content="Button" Height="29" HorizontalAlignment="Left" Margin="24,19,0,0" Name="button1" VerticalAlignment="Top" Width="90" Click="button1_Click" />
    <Popup Name="pop" Style="{StaticResource PopupStyle}" >
        <Grid Background="Red">
            <TextBlock Text="I am in pop up" />
        </Grid>
    </Popup>
</Grid>

和后面代码中的按钮单击事件处理程序以打开弹出窗口..

 private void button1_Click(object sender, RoutedEventArgs e)
    {
        pop.PlacementTarget = (Button)sender;
        pop.IsOpen = true;
    }

【讨论】:

    【解决方案2】:
    1. 为弹出窗口创建一个Style
    2. Trigger IsOpen -> true
    3. 使用Trigger.EnterActions 开始故事板。

    【讨论】:

    • 谢谢!可以解释一下吗?
    • 解释什么?请阅读文档,我认为这里没有什么要解释的......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 2018-01-15
    • 2017-01-02
    相关资源
    最近更新 更多