有没有办法从另一个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(); } } }
更多推荐
发布评论