开发者

Stop a Storyboard from another Window

i have a Storyboard in the MainWindow as follow :

    <Storyboard x:Key="OnMouseLeftButtonDown1">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"  RepeatBehavior="Forever" DecelerationRatio="1" Storyboard.TargetName="ellipse">
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

in the same window i have a button to call this StoryBoard and in the same time to open a new Window as follows :

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Storyboard sb= this.Resources["OnMouseLeftButtonDown2"] as Storyboard;
                if (sb != null)
                    sb.Begin(this);
                Window2 win = new Window2();
                win.Show();
    }

Now when it shows the Window2 i want stop the storyboard["OnMouseLeftButtonDown2"] with a button control ,do you have any suggestion how w开发者_C百科ork out this step?

Tahnsk so much for your attention,

Cheers


Hi I think you want to stop the Storyboard from your new Window - right? I would suggest some thing like this:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Storyboard sb= this.Resources["OnMouseLeftButtonDown2"] as Storyboard;
                if (sb != null)
                    sb.Begin(this);
                Window2 win = new Window2();
                win.StopStoryboardAction = () => sb.Stop();
                win.Show();
    }

Just give your new Window a public Property for the Action:

public Action StopStoryboardAction { get; set; }

And inside your new window just call

if (StopStoryboardAction != null) StopStoryboardAction();

This way you get some fewer dependencies.

PS:

  • I think you can also write win.StopStoryboardAction = sb.Stop
  • maybe you have to get the current dispatcher in the closure but I don't think so


Related posts - xaml and code behind

StoryBoard board = (StoryBoard)this.FindResource("OnMouseLeftButtonDown2"); 
board.stop(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜