Search code examples
c#wpfxamldata-bindingdatatrigger

DataBinding with datatriggers XAML with Boolean Local Variable


I am having trouble getting my tabitem to flash when the value for newCall is true. I think i have the Xaml correct but i am not sure how to bind it behind in code. When the variable new Call is set to true i would like my tabitem to flash.

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

Solution

  • Your ViewModel has to implement INotifyPropertyChanged. In the ViewModel add the following Code:

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

    and change Binding="{Binding newCall} to Binding="{Binding newCall, UpdateSourceTrigger=PropertyChanged}"

    Then, the TabItem will start flashing as soon as newCall is set to true