tags:

views:

378

answers:

1

Hi, I have a usercontrol that when I double click on it, I want it to zoom in, if it's not already. If it is, then the double click will zoom out on it. I can get it to work with code behind, but I can't get it to work in xaml. Here is the code behind that handle's the double click event.

void MyObjectMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (IsZoomedIn)
        {
            IsZoomedIn = false;
            //ZoomOutAnimation();
        }
        else
        {
            IsZoomedIn = true;
            //ZoomInAnimation();
        }
    }

then in my xaml:

<UserControl.RenderTransform>
    <TransformGroup>
        <RotateTransform />
        <ScaleTransform />
        <TranslateTransform />
    </TransformGroup>
</UserControl.RenderTransform>

<UserControl.Style>
    <Style>
        <Style.Triggers>
            <Trigger Property="local:MyObject.IsZoomedIn" Value="False">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="(UserControl.
                            RenderTransform).(TransformGroup.Children)[1].
                            (ScaleTransform.ScaleX)" To="1" Duration="0:0:.3" />
                            <DoubleAnimation Storyboard.TargetProperty="(UserControl.
                            RenderTransform).(TransformGroup.Children)[1].
                            (ScaleTransform.ScaleY)" To="1" Duration="0:0:.3" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
            <Trigger Property="local:MyObject.IsZoomedIn" Value="True">
                <Trigger.EnterActions>        
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="(UserControl.
                        RenderTransform).(TransformGroup.Children)[1].
                        (ScaleTransform.ScaleX)" To="2" Duration="0:0:.3" />
                        <DoubleAnimation Storyboard.TargetProperty="(UserControl.
                        RenderTransform).(TransformGroup.Children)[1].
                        (ScaleTransform.ScaleY)" To="2" Duration="0:0:.3" />
                    </Storyboard>
                </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Style>

Once it zooms in on my usercontrol, the zoom out animation doesn't work. Any help appreciated.
Thanks.

+3  A: 

Your animations are holding the values, and so the second animation isn't appearing even though the trigger is firing. Instead of having two separate triggers, you can use the Trigger.ExitActions like you are using the EnterActions.

<Trigger Property="IsZoomedIn"
   Value="True">
 <Trigger.EnterActions>
  <BeginStoryboard>
   <Storyboard >
    <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)"  
         To="2"
         Duration="0:0:.3" />
    <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)"
         To="2"
         Duration="0:0:.3" />
   </Storyboard>
  </BeginStoryboard>
 </Trigger.EnterActions>
 <Trigger.ExitActions>
  <BeginStoryboard>
   <Storyboard>
    <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)"
         To="1"
         Duration="0:0:.3" />
    <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)"
         To="1"
         Duration="0:0:.3" />
   </Storyboard>
  </BeginStoryboard>
 </Trigger.ExitActions>
</Trigger>
rmoore
Thanks! But I still don't quite understand why your code works. I know it might not be "correct" to think of it this way, but is it because EnterActions is more like "if true" and ExitActions is more like "else"?<br />Thanks.
jkidv
EnterActions and ExitActions are more akin to Initialize and Dispose then if/else. It *seems* to be happening because of the order of the triggers, for some reason they are unable to 'handoff' to each other and storyboards will force their values in the UI since they have one of the higher precedences. I don't use storyboards commonly enough to comment definitively on why it isn't working, but moving it to a single trigger, or explicitly stopping each storyboard in the exit actions fixes it. http://msdn.microsoft.com/en-us/library/ms743230.aspx
rmoore