有没有办法从另一个UserControl(Child)后面的代码访问UserControl(Parent)Storyboard。我有一个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();
}
}
}
答案 0 :(得分:0)
我能够解决问题,这里是更新的代码:
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();
}
}
}
&#13;