访问按钮样式的故事板

发布于 2024-10-21 10:06:00 字数 3393 浏览 2 评论 0原文

我的问题是我有一个 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;

我该如何继续? 非常感谢!

My problem is that I have a Button and I want to access the Storyboard, which is a part of the assigned style.

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

The style is very comprehensive so I'll post only a part of it:

<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>

I just want to get notified when the "ExpandedFull" animation has ended. Therefore, I thought I have to get the "expandStoryBoard" programmatically and add a Completed event handler.

The only thing I got working is to access the button's style at runtime:

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

How do I have to proceed?
Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚气少女 2024-10-28 10:06:00

理论上,您应该能够沿着按钮的视觉和逻辑树向下移动到情节提要,但如果您将模板中的 Grid 命名为“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 按钮以获取情节提要。

希望其中一些能起作用,或者至少有一点帮助。

In theory you should be able to go down the visual and logical tree of your button to get to the storyboard, but that is rather tedious, if you name the Grid in the template "grid", something like the following might work:

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.");

Another way that comes to mind is extracting your storyboard to a resource, but i am not sure if this will have any side effects, i.e.:

<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}"/>

Then you should be able to use FindResource on the button to get the storyboard.

Hopefully some of that works or at least helps a bit.

千鲤 2024-10-28 10:06:00

只需尝试使用 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;

    }

Just try with this with StoryBoard name "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;

    }
你不是我要的菜∠ 2024-10-28 10:06:00

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

在控件的资源部分定义 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)
{
   ...
}

If you add the Storyboard to the resources, you can set the event handler for the Timeline.Completed event in the XAML file and implement the handler in the corresponding class.

Define the Storyboard in the Resources section of your control like this:

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

Reference the Storyboard as a static resource:

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

Implement the Completed event handler in the corresponding class:

void OnExpandCompleted(object sender, EventArgs e)
{
   ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文