xaml : Retrigger opacity animation on multiple conditions
        Posted  
        
            by Sdry
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Sdry
        
        
        
        Published on 2010-03-25T15:14:33Z
        Indexed on 
            2010/03/25
            15:23 UTC
        
        
        Read the original article
        Hit count: 568
        
I have a problem figuring out how datatriggers and multidatatriggers work. I am trying to display a message, and depending on the type of message keep it displayed( + having a background), or having it fade out by a double animation on the opacity property (+ having a transparent background).
My xaml view has a game object as datacontext, which has a dependency property of type GameMessage, of which the constructor looks like this:
public GameMessage(bool containsMessage, string message, bool canFadeAway)
         {
              ContainsMessage = containsMessage;
              Message = message;
              CanFadeAway = canFadeAway;
         }
pretty straight forward, I want to display the message when ContainsMessage =equals true, and trigger a fade out animation if canFadeAway equals true. But also set the background based on canFadeAway.
<Canvas Name="messageCanvas" 
        Width="300" 
        Height="100"  
        Style="{StaticResource fadeInOut}">                        
         <TextBlock Name="txtMessage"  
                    Text="{Binding Path=GameMessage.Message}" 
                    Canvas.Top="25" Canvas.Left="0" 
                    Foreground="{StaticResource MessageForegroundBrush}">
          </TextBlock>
 </Canvas>
Now, the Style and triggers is where I get into trouble:
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                 <Condition  Binding="{Binding Path=GameMessage.ContainsMessage}" 
                                Value="True"/>
                 <Condition  Binding="{Binding  Path=GameMessage.CanFadeAway}"  
                                Value="False"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="Background" 
                        Value="{StaticResource MessageBackgroundBrush}" />
                <Setter Property="Opacity" 
                        Value="8" />
            </MultiDataTrigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                 <Condition  Binding="{Binding Path=GameMessage.ContainsMessage}"  
                                Value="True"/>
                 <Condition  Binding="{Binding  Path=GameMessage.CanFadeAway}"  
                                Value="True"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="Background" 
                        Value="Transparent" />
                <Setter Property="Opacity" 
                         Value="8" />
            </MultiDataTrigger>
            <DataTrigger  Binding="{Binding Path=GameMessage.CanFadeAway}"  
                          Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard >
                    <Storyboard BeginTime="0:0:0" >
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" 
                                         From="8"  To="0" 
                                         Duration="0:0:1" BeginTime="0:0:0" />     
                    </Storyboard>
                </BeginStoryboard>  
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                           To="8" Duration="0:0:0.1" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.ExitActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
The problem is in resetting the opacity when the GameMessage Property ( of type GameMessage) is of type true,msg",true, and gets replaced by a GameMessage object of the same kind. The opcacity remains 0, and messages only get restored again when I have a message of kind true,"msg,false.
After the animation, the opacity is 0, and where I would expect the second multidatatrigger to set it back to 8, and then have the animation performed by the Datatrigger, it doesnt.
What would be the best way to get this working ?
© Stack Overflow or respective owner