基于其他模型属性更改触发元素的动画/样式

时间:2012-08-07 14:48:14

标签: wpf xaml

我有一个绑定到模型属性的控件模板,比如说Property1。但是,如果更新Property2(无论值如何),我想要闪烁Property1绑定的元素的背景。我已经看过许多示例,其中DataTrigger可用于类似的东西,但在这种情况下,我不关心属性更改的值,只是它已经改变。

到目前为止,我有类似的东西:

<Style x:Key="QuotePriceCellStyle" TargetType="TextBlock">
...
...
    <DataTrigger Binding="{Binding Path=AskPrice, UpdateSourceTrigger=PropertyChanged}" >
        <DataTrigger.EnterActions>
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation From="Red" To="Transparent" Duration="0:0:2" Storyboard.TargetProperty="Background.Color" RepeatBehavior="1x"/>
                </Storyboard>
            </BeginStoryboard>
        </DataTrigger.EnterActions>
    </DataTrigger>
</Style>

<ControlTemplate x:Key="QuotePrice" >
    <TextBlock Style="{StaticResource QuotePriceCellStyle}" Text="{Binding QuotePrice}">
</ControlTemplate>

以上显然不能满足我的需要。 QuotePrice和AskPrice是模型的属性。关于如何在AskPrice更改时让QuotePrice单元格突出显示的任何想法?

2 个答案:

答案 0 :(得分:0)

您可以像这样使用DataTrigger的转换器:

public class FlashConverter : IValueConverter
{
    private object oldvalue;
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return false;
        if (oldvalue == value) return false;
        else
        {
            oldvalue = value;
            return true;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

您的数据触发器将是:

<DataTrigger Binding="{Binding Path=AskPrice, Converter={StaticResource FlashConverter1}, UpdateSourceTrigger=PropertyChanged}" Value="True">

所以在您的转换器中,您可以决定何时打开背景。

答案 1 :(得分:0)

我选择使用DataTrigger有条件地将两个单元格绑定到两个单元格并使用EventTrigger w / NotifyTargetUpdated设置为true来触发实际闪烁。