DataBinding与数据触发器XAML与布尔局部变量

时间:2015-05-30 21:00:39

标签: c# wpf xaml data-binding datatrigger

当newCall的值为true时,我无法让我的tabitem闪烁。我认为我有Xaml正确,但我不知道如何在代码中绑定它。当变量new Call设置为true时,我希望我的tabitem闪烁。

<TabItem.Style>
    <Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource MetroTabItem}">
        <Style.Resources>
            <Storyboard x:Key="flashAnimation" >
                <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" AutoReverse="True" Duration="0:0:0.5" RepeatBehavior="Forever" />
            </Storyboard>
        </Style.Resources>
        <Style.Triggers>
            <DataTrigger Binding="{Binding newCall}" Value="True">
                <DataTrigger.EnterActions>
                <BeginStoryboard Name="flash" Storyboard="{StaticResource flashAnimation}" />
                    </DataTrigger.EnterActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TabItem.Style>

1 个答案:

答案 0 :(得分:0)

您的ViewModel必须实现INotifyPropertyChanged。在ViewModel中添加以下代码:

private bool _newCall;

    public bool newCall
    {
        get { return _newCall; }
        set
        { 
            _newCall = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("newCall"));
            }
        }
    }

并将Binding="{Binding newCall}更改为Binding="{Binding newCall, UpdateSourceTrigger=PropertyChanged}"

然后,只要newCall设置为true,TabItem就会开始闪烁