C# WPF 动画标签内容未显示

标签 c# wpf storyboard label controltemplate

我正在尝试对标签的内容进行动画处理,以便执行以下操作:

Loading
Loading.
Loading..
Loading...

再次:

Loading
Loading.
Loading..
Loading...

诸如此类example .

代码下方:

<Label Grid.Row="2" x:Name="CurrentTask" 
       FontFamily="Microsoft Sans Serif" FontSize="18" FontStyle="Italic" FontWeight="Bold" 
       Foreground="White" Content="Loading">
    <Label.Template>
        <ControlTemplate>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Content" Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Label.Template>
</Label>

问题是标签内容在wpf窗口中不可见。它没有被显示。我做错了什么?

也在类型行中:

<DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>

我想将标签内容连接到点,而不是对值进行硬编码,我该怎么做?我不想在每个 DiscreteObjectKeyFrame 中重复“Loading”字符串。

更新: 我没有在 IsEnabled=True 上引发触发器,而是使用了 label.loaded,如下所示,例如在应用标签样式的情况下:

    <Label Grid.Row="2" x:Name="CurrentTask" 
           FontFamily="Microsoft Sans Serif" FontSize="18" FontStyle="Italic"  
           Foreground="White" Content="Loading">
        <Label.Style>
            <Style TargetType="Label">
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Label.Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>

最佳答案

除了触发器之外,您的 ControlTemplate 是空的,因此显然没有显示任何内容。

您可以添加 ContentPresenter,也可以删除 Storyboard.TargetName。您的触发器对设置为 false 的 IsEnabled 起作用,这看起来也很奇怪。

<Label.Template>
    <ControlTemplate TargetType="Label">

        <ContentPresenter/>

        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Label.Template>

但是,您实际上可能想要的是样式触发器而不是 ControlTemplate 触发器:

<Label ...>
    <Label.Style>
        <Style TargetType="Label">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/>
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
</Label>

对于Content中重复的“Loading”字符串,只需使用两个Label,一个带有固定的“Loading”字符串,另一个带有点,并调整其Padding。或者更好,两个 TextBlock:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Loading"/>
    <TextBlock>
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Text" Duration="00:00:00.8" RepeatBehavior="Forever">
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value=""/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="."/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value=".."/>
                                        <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="..."/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</StackPanel>

关于C# WPF 动画标签内容未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47815552/

相关文章:

c# - 使用反射在我的 ViewModel 中创建通用类型命令有什么缺点?

c# - 使用.NET读取oracle表,一个LONG类型的列总是返回空字符串,如何解决?

WPF:调用线程无法访问此对象,因为不同的线程拥有它

c# - 使 AvalonEdit MVVM 兼容

iphone - iOS6:如何通过Identifier连接Storyboard中的UIViewController?

c# - 按多个表分组并仍然可以访问原始查询?

c# - 防止多个客户端使用 cookie 重播/cookie

wpf - 如何向 DataGridTextColumn 添加工具提示

iOS 为 iPhone 4 和 5 制作通用应用程序

ios - 我的 XIB 中有电池 我无法方便地设计我的 View