添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a Style code define in ResourceDirectory to animate my popup. this is WPF code:

<Style x:Key="Hardwarepopups" TargetType="Popup">
    <Style.Triggers>
        <Trigger Property="IsOpen" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard >
                    <Storyboard>
                        <DoubleAnimation Duration="0:0:.3" Storyboard.TargetProperty="Width" From="0" To="100" AccelerationRatio=".1"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>

This is Popup:

<Popup Height="Auto" Width="Auto" Name="HardwareToolTip" StaysOpen="True" AllowsTransparency="True" Style="{StaticResource Hardwarepopups}">

It works well by handle all in XAML. I have decided to convert it to C# code like this:

void SetHarwareStyle(Popup B) {
    var RightToLeft = new DoubleAnimation()
    From = 0,
    To = 100,
    Duration = TimeSpan.FromMilliseconds(300),
    AccelerationRatio = 0.1
    Storyboard.SetTargetProperty(RightToLeft, new PropertyPath(WidthProperty));
    Storyboard C = new Storyboard();
    C.Children.Add(RightToLeft);
    var action = new BeginStoryboard();
    action.Storyboard = C;
    Trigger A = new Trigger { Property = Popup.IsOpenProperty, Value = true };
    A.EnterActions.Add(action);
    B.Triggers.Add(A);

But this line B.Triggers.Add(A); gives the error System.InvalidOperationException: 'Triggers collection members must be of type EventTrigger.' How can I solve this problem? The propose of this conversion is to change To property of DoubleAnimation in runtime.

A different solution could be using data binding to set the To property: To="{Binding Width, ElementName=MyWindow}" – Emond Jul 11, 2018 at 17:54 I tried this before, this is the error: Cannot freeze this Storyboard timeline tree for use across threads. According this stackoverflow.com/questions/1669750/… I decided to use code behind. – Mohammad Farahi Jul 11, 2018 at 18:10

The code in the question does not reflect the XAML completely: the style is missing.

I renamed some of the variables to make it easier to read (and prevent mistakes like these)

BTW: the rightToLeftAnimation should be named leftToRightAnimation.

void SetHarwareStyle(Popup popup)
    var rightToLeftAnimation = new DoubleAnimation()
        From = 0,
        To = 100,
        Duration = TimeSpan.FromMilliseconds(300),
        AccelerationRatio = 0.1
    Storyboard.SetTargetProperty(rightToLeftAnimation, new PropertyPath(WidthProperty));
    var storyboard = new Storyboard();
    storyboard.Children.Add(rightToLeftAnimation);
    var beginStoryboard = new BeginStoryboard();
    beginStoryboard.Storyboard = storyboard;
    var trigger = new Trigger { Property = Popup.IsOpenProperty, Value = true };
    trigger.EnterActions.Add(beginStoryboard);
    var style= new Style();
    style.TargetType = typeof(Popup);
    style.Triggers.Add(trigger);
    popup.Style = style;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.