3

我的问题是我有一个Button并且我想访问它Storyboard,它是指定样式的一部分。

<Button x:Name="pictureFolderButton" Content="Pictures" Style="{StaticResource ImageTileButtonStyle}" Click="pictureFolderButton_Click" />

风格很全面,所以我只贴一部分:

<Style x:Key="ImageTileButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <ControlTemplate.Resources>
                            <Storyboard x:Key="OnLoaded1"/>
                        </ControlTemplate.Resources>
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    ...
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="AnimationStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0:0:1">
                                            <VisualTransition.GeneratedEasingFunction>
                                                <CircleEase EasingMode="EaseOut"/>
                                            </VisualTransition.GeneratedEasingFunction>
                                        </VisualTransition>
                                    </VisualStateGroup.Transitions>

                                    <VisualState x:Name="ExpandedFull">
                                        <Storyboard x:Name="expandStoryBoard" >
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="border1">

                                                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="130"/>
                                                <EasingDoubleKeyFrame KeyTime="0:0:3" Value="130"/>
                                                <EasingDoubleKeyFrame KeyTime="0:0:4" Value="47"/>
                                                <EasingDoubleKeyFrame KeyTime="0:0:8" Value="47"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

                            <ContentPresenter RecognizesAccessKey="True" VerticalAlignment="Stretch" Margin="0,47,0,0" />

                        </Grid>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

我只想在“ExpandedFull”动画结束时收到通知。因此,我认为我必须以编程方式获取“expandStoryBoard”并添加一个Completed事件处理程序。

我唯一要做的就是在运行时访问按钮的样式:

Style style = pictureFolderButton.FindResource("ImageTileButtonStyle") as Style;

我该如何进行?非常感谢!

4

3 回答 3

2

只需尝试使用StoryBoard名称“OnLoaded1”:

 <Button Height="75" Width="120" Style="{StaticResource ImageTileButtonStyle}" Click="Button_Click" >Hello</Button>


  private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button btn=(Button)sender;

        Storyboard stb = btn.TryFindResource("OnLoaded1") as Storyboard;

    }
于 2011-03-12T11:34:24.980 回答
2

从理论上讲,您应该能够沿着按钮的视觉和逻辑树进入情节提要,但这相当乏味,如果您Grid在模板中将其命名为“网格”,则可能会出现以下情况

Grid grid = pictureFolderButton.FindName("grid") as Grid;
IList groups = VisualStateManager.GetVisualStateGroups(grid);
VisualStateGroup targetGroup = null;
foreach (var group in groups)
{
    if (group is VisualStateGroup && (group as VisualStateGroup).Name == "AnimationStates")
    {
        targetGroup = group as VisualStateGroup;
        break;
    }
}
if (targetGroup != null)
{
    IList states = targetGroup.States;
    VisualState targetState = null;
    foreach (var state in states)
    {
        if (state is VisualState && (state as VisualState).Name == "ExpandedFull")
        {
            targetState = state as VisualState;
            break;
        }
    }
    if (targetState != null)
    {
        targetState.Storyboard.Completed += new EventHandler(Expansion_Completed);
    }
    else throw new Exception("VisualState not found.");
}
else throw new Exception("VisualStateGroup not found.");

想到的另一种方法是将故事板提取到资源中,但我不确定这是否会产生任何副作用,即:

<ControlTemplate.Resources>
    ...
    <Storyboard x:Key="expandStoryBoard" x:Name="expandStoryBoard">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="border1">
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="130"/>
            <EasingDoubleKeyFrame KeyTime="0:0:3" Value="130"/>
            <EasingDoubleKeyFrame KeyTime="0:0:4" Value="47"/>
            <EasingDoubleKeyFrame KeyTime="0:0:8" Value="47"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</ControlTemplate.Resources>
...
<VisualState x:Name="ExpandedFull" Storyboard="{StaticResource expandStoryBoard}"/>

然后你应该能够使用FindResource按钮来获取故事板。

希望其中一些有效或至少有所帮助。

于 2011-03-12T13:50:26.427 回答
0

如果添加Storyboard到资源中,则可以Timeline.Completed在 XAML 文件中设置事件的事件处理程序,并在相应的类中实现处理程序。

Storyboard在控件的资源部分中定义如下:

<UserControl.Resources>
  <Storyboard x:Key="expandStoryBoard" Completed="OnExpandCompleted"> ... </Storyboard>
  ...
</UserControl.Resources>

将 引用Storyboard为静态资源:

<VisualState x:Name="ExpandedFull" Storyboard="{StaticResource expandStoryBoard}" />

Completed在相应的类中实现事件处理程序:

void OnExpandCompleted(object sender, EventArgs e)
{
   ...
}
于 2013-03-07T03:14:01.680 回答