【问题标题】:DataTrigger don't seems to fireDataTrigger 似乎没有触发
【发布时间】:2012-06-12 11:19:28
【问题描述】:

我想创建一个数据触发器,让我的页面闪烁(从透明变为红色)。所以我创建了一个 DataTrigger 来监听我的视图模型中的一个布尔标志。该标志应指示何时需要提醒用户。在这种情况下,我的页面将从透明闪烁到红色。

我很确定我已经以正确的方式实现了数据触发器,但是我的应用什么也没做 - 没有错误,没有闪烁......所以我一定错过了一些东西。

<Style x:Key="ReminderPage" TargetType="{x:Type ViewTemplates:TpApplicationBarView}" BasedOn="{StaticResource TpApplicationBarViewStyle}">
    <Style.Triggers>

        <!-- Reminder animation, when the time comes to remind the user -->
        <DataTrigger Binding="{Binding IndicateReminderAnimation}" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard x:Name="Blink">
                        <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                                        AutoReverse="True" 
                                        From="Transparent" 
                                        To="Red" 
                                        Duration="0:0:1" 
                                        RepeatBehavior="Forever">
                        </ColorAnimation >
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>

        <DataTrigger Binding="{Binding IndicateReminderAnimation}" Value="False">
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                                        AutoReverse="False" 
                                        To="Transparent" 
                                        Duration="0:0:1">
                        </ColorAnimation >
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

那么,我做错了什么?

更新:我可以在输出窗口中看到以下消息:

System.Windows.Media.Animation Warning: 6 : Unable to perform action because 
the specified Storyboard was never applied to this object for interactive control.;        
Action='Stop'; Storyboard='System.Windows.Media.Animation.Storyboard'; 
Storyboard.HashCode='61356140'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; 
TargetElement='System.Windows.Media.Animation.Storyboard'; TargetElement.HashCode='61356140'; 
TargetElement.Type='System.Windows.Media.Animation.Storyboard'

Update2: 谷歌搜索后我发现这是 UI 线程的问题。因此,每当我设置绑定属性时,我都会进行调度程序调用。但即使有这个技巧,也没有彩色动画。但是输出窗口中的错误似乎消失了。因此,我正在寻找有关如何修复动画的更多想法。

Update3:设置页面的背景颜色似乎是一个普遍的问题。但这真的很奇怪。页面放置在 NavigationFrame 中。设置导航框架的背景颜色会改变应用程序的颜色,但设置页面的背景颜色(即使没有任何动画)不会改变任何东西。

【问题讨论】:

  • 您是否为属性IndicateReminderAnimation 实施了INotifyPropertyChanged?运行时检查VS中的输出窗口,有没有绑定错误?
  • 嗯。我实现了 INotifyPropertyChanged。但是我查看了输出并发生了​​以下错误: System.Windows.Media.Animation 警告:6:无法执行操作,因为指定的情节提要从未应用于此对象进行交互式控制。行动='停止'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='61356140'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='System.Windows.Media.Animation.Storyboard'; TargetElement.HashCode='61356140'; TargetElement.Type='System.Windows.Media.Animation.Storyboard'

标签: wpf datatrigger coloranimation


【解决方案1】:

我认为你必须设置动画目标,像这样 -

Storyboard.TargetName="yourWindowName"

您可能已经检查过了,但请确保将正确的对象设置为您的 TpApplicationBarView 的 DataContext(具有 IndicateReminderAnimation 属性)。

【讨论】:

  • 抱歉,我无法在样式中指定目标名称。
【解决方案2】:

我发现了这个错误 - 或者更好的两个错误。

1.) 似乎无法更改放置在导航框架内的页面的背景颜色。

首先是将绑定和事件移动到 MainWindow 本身(wpf 窗口类)

2.) 包含数据触发器的样式不起作用。谷歌搜索后,我找到了我正在寻找的工作解决方案。

<Storyboard x:Key="RemindUser" >
    <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                    AutoReverse="True" 
                    From="Transparent" 
                    To="{StaticResource WinAccentBackgroundColor}" 
                    Duration="0:0:1" 
                    RepeatBehavior="Forever">
    </ColorAnimation >
</Storyboard>

<Storyboard x:Key="StopRemindUser">
    <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                    AutoReverse="True" 
                    To="Transparent" 
                    Duration="0:0:1">
    </ColorAnimation >
</Storyboard>

<Style x:Key="ReminderWindow" TargetType="{x:Type Metro:SnappedTransparentWindow}" BasedOn="{StaticResource TransparentWindow}">
    <Style.Triggers>

        <!-- Reminder animation, when the time comes to remind the user -->
        <DataTrigger Binding="{Binding IndicateReminderAnimation}" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource RemindUser}"/>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <BeginStoryboard Storyboard="{StaticResource StopRemindUser}"/>
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

关键是将绑定和故事板分成不同的部分。

【讨论】:

  • @BitKFu:能不能解释一下第二点,不清楚是什么意思?我认为这是 Page 的问题,我在窗口中尝试了您的样式,它工作正常。
  • 是的,我知道。这真的很奇怪。因为你的评论,我自己做了一个测试项目,真的,我也可以设置页面的背景颜色。在我的应用程序中无法正常工作......太奇怪了......如果我有更多时间,我会尝试重现它,因为我也有兴趣知道这个谜团的答案。
猜你喜欢
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多