从另一个UserControl后面的代码启动UserControl Storyboard(Starting UserControl Storyboard from code behind anothe

编程入门 行业动态 更新时间:2024-10-10 15:24:05
从另一个UserControl后面的代码启动UserControl Storyboard(Starting UserControl Storyboard from code behind another UserControl)

有没有办法从另一个UserControl(子)的代码后面访问UserControl(父)故事板。 我有一个UserControl(父用户控件),其中包含另一个UserControl(子用户控件)。 父用户控件有一个由父用户控件使用的故事板,现在,我想从子用户控件后面的代码开始播放该故事板。 有没有办法实现这一目标。 这是示例代码:

家长用户控制:

    <UserControl.Resources>
        <Storyboard x:Key="sbShowEditLetter">
            <DoubleAnimation 
											Storyboard.TargetName="grdOptions"
											Storyboard.TargetProperty="(FrameworkElement.Opacity)" 
											From="0" To="10" 
											Duration="0:0:5">
            </DoubleAnimation>
            <DoubleAnimation
                                                Storyboard.TargetName="grdOptions"
											    Storyboard.TargetProperty="(FrameworkElement.Width)" 
											    From="0" To="420" 
											    Duration="0">
            </DoubleAnimation>
        </Storyboard>
    </UserControl.Resources> 
  
 

                        <Button x:Name="btnAdd" Content="Add ...">
                            <Button.Triggers>
                                <EventTrigger RoutedEvent="ButtonBase.Click">
                                    <BeginStoryboard Storyboard="{StaticResource sbShowEditLetter}">
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Button.Triggers>
                        </Button>
        
  
 

儿童用户控制:

    private void dgLetters_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement originalSender = e.OriginalSource as FrameworkElement;
        if (originalSender != null)
        {
            var row = originalSender.ParentOfType<GridViewRow>();
            if (row != null)
            {

                //access Parent User Control Storyboard
                Storyboard sb = this.FindResource("sbShowEditLetter") as Storyboard;
                sb.Begin();
            }
        }
    } 
  
 

Is there a way to access UserControl (Parent) Storyboard from a code behind of another UserControl (Child). I have a UserControl (Parent User Control) that contains another UserControl (Child User Control). Parent User Control have a Storyboard that is being utilized by the Parent User Control, and now, I would like to start playing that Storyboard from a code behind of Child User Control. Is there a way to achieve that. Here is sample code:

Parent User Control:

    <UserControl.Resources>
        <Storyboard x:Key="sbShowEditLetter">
            <DoubleAnimation 
											Storyboard.TargetName="grdOptions"
											Storyboard.TargetProperty="(FrameworkElement.Opacity)" 
											From="0" To="10" 
											Duration="0:0:5">
            </DoubleAnimation>
            <DoubleAnimation
                                                Storyboard.TargetName="grdOptions"
											    Storyboard.TargetProperty="(FrameworkElement.Width)" 
											    From="0" To="420" 
											    Duration="0">
            </DoubleAnimation>
        </Storyboard>
    </UserControl.Resources> 
  
 

                        <Button x:Name="btnAdd" Content="Add ...">
                            <Button.Triggers>
                                <EventTrigger RoutedEvent="ButtonBase.Click">
                                    <BeginStoryboard Storyboard="{StaticResource sbShowEditLetter}">
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Button.Triggers>
                        </Button>
        
  
 

Child User Control:

    private void dgLetters_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement originalSender = e.OriginalSource as FrameworkElement;
        if (originalSender != null)
        {
            var row = originalSender.ParentOfType<GridViewRow>();
            if (row != null)
            {

                //access Parent User Control Storyboard
                Storyboard sb = this.FindResource("sbShowEditLetter") as Storyboard;
                sb.Begin();
            }
        }
    } 
  
 

最满意答案

我能够解决这个问题,这里是更新的代码:

        private void dgLetters_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement originalSender = e.OriginalSource as FrameworkElement;
            if (originalSender != null)
            {
                var row = originalSender.ParentOfType<GridViewRow>();
                if (row != null)
                {

                    DependencyObject ucParent = this.Parent;

                    while (!(ucParent is UserControl))
                    {
                        ucParent = LogicalTreeHelper.GetParent(ucParent);
                    }

                    MainWindow2 parentUserControl = (MainWindow2) ucParent;
                    parentUserControl.vm.ExecuteEdit();

                    Storyboard sb = this.FindResource("sbShowEditLetter") as Storyboard;
                    sb.Begin();
                }
            }
        } 
  
 

I was able to resolve the issue, here is the updated code behind:

        private void dgLetters_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement originalSender = e.OriginalSource as FrameworkElement;
            if (originalSender != null)
            {
                var row = originalSender.ParentOfType<GridViewRow>();
                if (row != null)
                {

                    DependencyObject ucParent = this.Parent;

                    while (!(ucParent is UserControl))
                    {
                        ucParent = LogicalTreeHelper.GetParent(ucParent);
                    }

                    MainWindow2 parentUserControl = (MainWindow2) ucParent;
                    parentUserControl.vm.ExecuteEdit();

                    Storyboard sb = this.FindResource("sbShowEditLetter") as Storyboard;
                    sb.Begin();
                }
            }
        } 
  
 

更多推荐

本文发布于:2023-07-27 15:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292358.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   UserControl   Storyboard   code   Starting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!