Prevent TabItem event from triggering WPF - wpf

<TabControl x:Name="tabControl" TabStripPlacement="Top" BorderBrush="White" FontSize="14" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="98" Margin="0,2,0,0" VerticalAlignment="Top" Width="992" FontFamily="Segoe UI">
<TabControl.Resources>
<Style TargetType="TabItem">
<Style.Triggers>
<Trigger Property="TabItem.IsSelected" Value="True">
<Setter Property="TabItem.Foreground" Value="#FF0090D3"/>
<Setter Property="TabItem.FontSize" Value="14"/>
<Setter Property="TabItem.FontFamily" Value="Segoe Ui SemiBold"/>
</Trigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Border Name="Border" BorderThickness="0,0,0,0" BorderBrush="White" Background="White" CornerRadius="13,13,13,13" Margin="1,0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="10,2"/>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<ParallelTimeline>
<ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.Background).Color" From="White" To="LightGray" Duration="0:0:0.2"/>
</ParallelTimeline>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<ParallelTimeline>
<ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.Background).Color" From="LightGray" To="White" Duration="0:0:0.2"/>
</ParallelTimeline>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem Width="190" Visibility="Hidden" />
<TabItem Height="25" IsSelected="True" Width="63" Header="Home">
<Canvas>
<Rectangle x:name="rect1" Fill="#FFF4F4F5" Height="59" Width="58" Canvas.Top="3" Canvas.Left="1">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Style.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Fill.Color" To="LightGray" Duration="0:0:0.2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Fill.Color" To="White" Duration="0:0:0.2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</Canvas>
</TabItem>
<TabItem Width="63" Header="Tools"/>
<TabItem Width="76" Header="Add-ons" />
</TabControl>
The code allows me to change the tab header color when the mouse is over the header.However,the problem is....when mouse is over a content inside a tabitem,the MouseEnter event of both the TabItem and the content fires.Which results in : If i hover over a content(eg. a rectangle that i'm using),the MouseEnter event of the content/control is fired.But it also fires the 'MouseEnter' event of the tabItem...So the tabItem/TabHeader chages it's color even when i don't hover over the header/TabItem rather when i hover over a control/content inside the tabitem...How do i prevent this ?
Update
I found this code :
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true" />
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="false"/>
<Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Value="Top"/>
from here.But where should i put it ?

Damn!! It was reallllyyy easy! Just changed :
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
To :
<ControlTemplate.Triggers>
<EventTrigger SourceName="ContentSite" RoutedEvent="MouseEnter">

Related

WPF Datatrigger ColorAnimation not working

I am currently working on my own Themes for all the wpf controls.
I can't really get the Checkbox Usercontrol working.
I'm trying to get a smooth color fade in / fade out when checking the checkbox.
This is the relevant part of my code in my ressource dictionary:
<Border x:Name="checkbox" CornerRadius="5" Width="18" Height="18" BorderThickness="1" BorderBrush="Black">
<!-- Checkmark -->
<TextBlock Text="" ClipToBounds="True" FontFamily="Segoe MDL2 Assets" HorizontalAlignment="Left"
Background="Transparent" Foreground="White" FontSize="15" FontWeight="ExtraBold"/>
<Border.Style>
<Style TargetType="Border">
<!-- Animations (Color fade in and out) -->
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#006092" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked}" Value="False">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#FFFFFF" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
The fade out works (Value=false), but the fade in never triggers. I added a "From" value to confirm / test this, it doesn't trigger at all.
All help would be much appreciated!
Kind Regards
The proper way is to use the EnterAction and ExitAction of the Trigger and to move the trigger from the Style to the ControlTemplate. Also note how the Border properties are wired to the templated parent to allow local values having an effect.
<CheckBox IsChecked="True" BorderThickness="1" BorderBrush="Black">
<CheckBox.Template>
<ControlTemplate TargetType="CheckBox">
<Border x:Name="checkbox"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="5"
Width="18" Height="18" >
<!-- Checkmark -->
<TextBlock Text="" ClipToBounds="True" FontFamily="Segoe MDL2 Assets" HorizontalAlignment="Left"
Background="Transparent" Foreground="White" FontSize="15" FontWeight="ExtraBold"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#006092" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#FFFFFF" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>

How to apply WPF stype to children of button template

I'm new to WPF and I would like to change to color of the text and icon of a button template. But it seems I can only change the opacity.
I guess I should apply the style to the children of the button but I don't know how.
Here is the template:
<Button x:Name="btnApp1" Width="56" Height="66" Margin="0,0,0,0" Style="{StaticResource AppButton}">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<iconPacks:PackIconMaterial Kind="StarOutline" Width="48" Height="48" VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="#FFFFFFFF" />
<TextBlock x:Name="tButton" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="#FFFFFFFF" FontWeight="Bold">PVIE</TextBlock>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
And here is the style:
<Style x:Key="AppButton" TargetType="{x:Type Button}">
<Setter Property="Opacity" Value="0.25" />
<Setter Property="Foreground" Value="#FFFF9966" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0:0.3" />
<!--<ColorAnimation Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" To="Green" Duration="0:0:0:0.3" />-->
<ColorAnimationUsingKeyFrames Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="Green" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.25" Duration="0:0:0:0.3" />
<!--<ColorAnimation Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" To="White" Duration="0:0:0:0.3" />-->
<ColorAnimationUsingKeyFrames Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="White" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
Thanks for your help.
Bind the Foreground property of the TextBlock and the Icon in the ControlTemplate using a {TemplateBinding}:
<Button x:Name="btnApp1" Width="56" Height="66" Margin="0,0,0,0" Style="{StaticResource AppButton}">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<iconPacks:PackIconMaterial Kind="StarOutline" Width="48" Height="48" VerticalAlignment="Top" HorizontalAlignment="Center"
Foreground="{TemplateBinding Foreground}" />
<TextBlock x:Name="tButton" HorizontalAlignment="Center" VerticalAlignment="Bottom"
Foreground="{TemplateBinding Foreground}" FontWeight="Bold">PVIE</TextBlock>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>

Toggle WPF animation on button click

I want to implement the button expanding animation where when user hovers over the top button, a sneak peak of the other functions each with their own button will be displayed below that top button on which the user hovered initially. I wrote the below XAML to achieve that and it is working as expected.
XAML:
<Button Name="panel" Margin="0,40,0,0">
<Button.Template>
<ControlTemplate x:Name="abc">
<Grid>
<Button Width="150" Name="addButton" Height="30" Grid.Column="1" VerticalAlignment="Top" FontSize="20" Margin="0,5,10,0" Foreground="White" VerticalContentAlignment="Stretch" Content="+" Click="Button_Click">
<Button.Template>
<ControlTemplate x:Name="addBtn" TargetType="Button">
<StackPanel Orientation="Horizontal" >
<TextBlock Name="addFavTxtBlock" Foreground="LightGray" FontWeight="SemiBold" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Add new task" Opacity="0" FontSize="14" Width="90" Visibility="Hidden">
</TextBlock>
<Grid>
<Ellipse Width="30" Stroke="#00a1f1" Name="btnEllipse"
StrokeThickness="2" Fill="#00a1f1">
</Ellipse>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Top"/>
<Grid.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="1"/>
</Grid.Effect>
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Width" TargetName="btnEllipse" Value="28"></Setter>
<Setter Property="Height" TargetName="btnEllipse" Value="28"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="addFavTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
<!--Trigger Property="IsMouseOver" Value="False">
<Setter Property="Visibility" TargetName="addFavTxtBlock" Value="Hidden"></Setter>
</-->
<Trigger Property="Margin" Value="0,45,10,0">
<Setter Property="Visibility" TargetName="addFavTxtBlock" Value="Visible"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="addFavTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="addFavTxtBlock" Storyboard.TargetProperty="Opacity" From="50" To="0" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<Button Width="150" Name="remButton" Height="30" Grid.Column="1" VerticalAlignment="Top" FontSize="20" Margin="0,5,10,0" Foreground="White" VerticalContentAlignment="Stretch" Content="-" Click="Button_Click">
<Button.Template>
<ControlTemplate x:Name="addBtn" TargetType="Button">
<StackPanel Orientation="Horizontal" >
<TextBlock Name="remFavTxtBlock" Foreground="LightGray" FontWeight="SemiBold" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Remove task" Opacity="0" FontSize="14" Width="90" Visibility="Hidden">
</TextBlock>
<Grid>
<Ellipse Width="30" Stroke="#00a1f1" Name="btnEllipse"
StrokeThickness="2" Fill="#00a1f1">
</Ellipse>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Top"/>
<Grid.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="1"/>
</Grid.Effect>
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Width" TargetName="btnEllipse" Value="28"></Setter>
<Setter Property="Height" TargetName="btnEllipse" Value="28"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="remFavTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="remFavTxtBlock" Storyboard.TargetProperty="Opacity" From="50" To="0" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="Margin" Value="0,85,10,0">
<Setter Property="Visibility" TargetName="remFavTxtBlock" Value="Visible"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="remFavTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="remFavTxtBlock" Storyboard.TargetProperty="Opacity" From="50" To="0" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<Button Width="150" Name="editButton" Height="30" Grid.Column="1" VerticalAlignment="Top" FontSize="20" Margin="0,5,10,0" Foreground="White" VerticalContentAlignment="Stretch" Content="..." Click="Button_Click">
<Button.Template>
<ControlTemplate x:Name="editBtn" TargetType="Button">
<StackPanel Orientation="Horizontal" >
<TextBlock Name="editTxtBlock" Foreground="LightGray" FontWeight="SemiBold" VerticalAlignment="Center" HorizontalAlignment="Left" Opacity="0" FontSize="14" Width="90">
</TextBlock>
<Grid>
<Ellipse Width="30" Stroke="#00a1f1" Name="btnEllipse"
StrokeThickness="2" Fill="#00a1f1">
</Ellipse>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Top"/>
<Grid.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="1"/>
</Grid.Effect>
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Width" TargetName="btnEllipse" Value="28"></Setter>
<Setter Property="Height" TargetName="btnEllipse" Value="28"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="editTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Visibility" TargetName="editTxtBlock" Value="Hidden"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=editButton}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="enterBoard">
<Storyboard>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetName="remButton" Storyboard.TargetProperty="Margin" BeginTime="00:00:00">
<SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,5,10,0" />
<SplineThicknessKeyFrame KeyTime="00:00:00.05" Value="0,85,10,0" />
</ThicknessAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetName="addButton" Storyboard.TargetProperty="Margin" BeginTime="00:00:00">
<SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,5,10,0" />
<SplineThicknessKeyFrame KeyTime="00:00:00.05" Value="0,45,10,0" />
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard Name="exitBoard">
<Storyboard>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetName="remButton" Storyboard.TargetProperty="Margin" BeginTime="00:00:00">
<SplineThicknessKeyFrame KeyTime="00:00:00.05" Value="0,5,10,0" />
<SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,85,10,0" />
</ThicknessAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetName="addButton" Storyboard.TargetProperty="Margin" BeginTime="00:00:00">
<SplineThicknessKeyFrame KeyTime="00:00:00.05" Value="0,5,10,0" />
<SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,45,10,0" />
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
Initial Screenshot:
When the user hovers on the blue button, two additional buttons will drop from behind this blue button. It is difficult to take screenshot for that as those two buttons will be displayed only until the user is hovering on the the blue button. This is exactly my problem.
If the user wants to click on the two additional buttons which come down, the user will have to move the cursor away from the blue button and immediately these two additional buttons will go away and hide behind the blue button and thus escaping the user's click. How can I overcome this?
Is there anyway I can overwrite the Trigger.ExitActions if the user clicks on the blue button instead of hovering on it? Or any other event which I can fire up to disable the ExitActions for a while?
I am trying to create a click event for the blue button and handle the required behaviour in the code behind but if there are any other better approaches, please let me know.
Either use VisualStates one for Normal and another for Expanded state.
Or, use ToggleButton instead of Button , in case of which your code would look like this :
<Button Name="panel" Margin="0,40,10,31">
<Button.Template>
<ControlTemplate x:Name="abc">
<Grid>
<Button Width="150" Name="addButton" Height="30" Grid.Column="1" VerticalAlignment="Top" FontSize="20" Margin="0,5,10,0" Foreground="White" VerticalContentAlignment="Stretch" Content="+" >
<Button.Template>
<ControlTemplate x:Name="addBtn" TargetType="Button">
<StackPanel Orientation="Horizontal" >
<TextBlock Name="addFavTxtBlock" Foreground="LightGray" FontWeight="SemiBold" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Add new task" Opacity="0" FontSize="14" Width="90" Visibility="Hidden">
</TextBlock>
<Grid>
<Ellipse Width="30" Stroke="#00a1f1" Name="btnEllipse"
StrokeThickness="2" Fill="#00a1f1">
</Ellipse>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Top"/>
<Grid.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="1"/>
</Grid.Effect>
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Width" TargetName="btnEllipse" Value="28"></Setter>
<Setter Property="Height" TargetName="btnEllipse" Value="28"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="addFavTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
<!--Trigger Property="IsMouseOver" Value="False">
<Setter Property="Visibility" TargetName="addFavTxtBlock" Value="Hidden"></Setter>
</-->
<Trigger Property="Margin" Value="0,45,10,0">
<Setter Property="Visibility" TargetName="addFavTxtBlock" Value="Visible"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="addFavTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="addFavTxtBlock" Storyboard.TargetProperty="Opacity" From="50" To="0" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<Button Width="150" Name="remButton" Height="30" Grid.Column="1" VerticalAlignment="Top" FontSize="20" Margin="0,5,10,0" Foreground="White" VerticalContentAlignment="Stretch" Content="-">
<Button.Template>
<ControlTemplate x:Name="addBtn" TargetType="Button">
<StackPanel Orientation="Horizontal" >
<TextBlock Name="remFavTxtBlock" Foreground="LightGray" FontWeight="SemiBold" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Remove task" Opacity="0" FontSize="14" Width="90" Visibility="Hidden">
</TextBlock>
<Grid>
<Ellipse Width="30" Stroke="#00a1f1" Name="btnEllipse"
StrokeThickness="2" Fill="#00a1f1">
</Ellipse>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Top"/>
<Grid.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="1"/>
</Grid.Effect>
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Width" TargetName="btnEllipse" Value="28"></Setter>
<Setter Property="Height" TargetName="btnEllipse" Value="28"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="remFavTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="remFavTxtBlock" Storyboard.TargetProperty="Opacity" From="50" To="0" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="Margin" Value="0,85,10,0">
<Setter Property="Visibility" TargetName="remFavTxtBlock" Value="Visible"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="remFavTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="remFavTxtBlock" Storyboard.TargetProperty="Opacity" From="50" To="0" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<ToggleButton Width="150" Name="editButton" Height="30" Grid.Column="1" VerticalAlignment="Top" FontSize="20" Margin="0,5,10,0" Foreground="White" VerticalContentAlignment="Stretch" Content="...">
<ToggleButton.Template>
<ControlTemplate x:Name="editBtn" TargetType="ToggleButton">
<StackPanel Orientation="Horizontal" >
<TextBlock Name="editTxtBlock" Foreground="LightGray" FontWeight="SemiBold" VerticalAlignment="Center" HorizontalAlignment="Left" Opacity="0" FontSize="14" Width="90">
</TextBlock>
<Grid>
<Ellipse Width="30" Stroke="#00a1f1" Name="btnEllipse"
StrokeThickness="2" Fill="#00a1f1">
</Ellipse>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Top"/>
<Grid.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="1"/>
</Grid.Effect>
</Grid>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Width" TargetName="btnEllipse" Value="28"></Setter>
<Setter Property="Height" TargetName="btnEllipse" Value="28"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="editTxtBlock" Storyboard.TargetProperty="Opacity" From="0" To="50" Duration="00:00:00.03"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Visibility" TargetName="editTxtBlock" Value="Hidden"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=editButton}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="enterBoard">
<Storyboard FillBehavior="HoldEnd">
<ThicknessAnimationUsingKeyFrames Storyboard.TargetName="remButton" Storyboard.TargetProperty="Margin" BeginTime="00:00:00">
<SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,5,10,0" />
<SplineThicknessKeyFrame KeyTime="00:00:00.05" Value="0,85,10,0" />
</ThicknessAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetName="addButton" Storyboard.TargetProperty="Margin" BeginTime="00:00:00">
<SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,5,10,0" />
<SplineThicknessKeyFrame KeyTime="00:00:00.05" Value="0,45,10,0" />
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard Name="exitBoard">
<Storyboard>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetName="remButton" Storyboard.TargetProperty="Margin" BeginTime="00:00:00">
<SplineThicknessKeyFrame KeyTime="00:00:00.05" Value="0,5,10,0" />
<SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,85,10,0" />
</ThicknessAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetName="addButton" Storyboard.TargetProperty="Margin" BeginTime="00:00:00">
<SplineThicknessKeyFrame KeyTime="00:00:00.05" Value="0,5,10,0" />
<SplineThicknessKeyFrame KeyTime="00:00:00" Value="0,45,10,0" />
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>

Animation inside DataTrigger won't run a second time

This is the code:
<Grid>
<Ellipse Fill="Turquoise" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100">
<Ellipse.Style>
<Style TargetType="Ellipse">
<Style.Setters>
<Setter Property="Ellipse.RenderTransform">
<Setter.Value>
<TranslateTransform X="0" Y="50"/>
</Setter.Value>
</Setter>
</Style.Setters>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=rectRight, Path=IsMouseOver}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard TargetProperty="RenderTransform.(TranslateTransform.X)">
<!--<DoubleAnimation To="{Binding Path=Width}" Duration="0:0:1"/>--> <!--Doesn't work inside a Style Trigger-->
<DoubleAnimation To="250" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=rectLeft, Path=IsMouseOver}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard TargetProperty="RenderTransform.(TranslateTransform.X)">
<DoubleAnimation To="45" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Bottom" Orientation="Horizontal">
<Rectangle Name="rectLeft" Fill="Gray" Height="100" Stroke="Black" Width="100" Margin="10"/>
<Rectangle Name="rectRight" Fill="Gray" Height="100" Stroke="Black" Width="100" Margin="10"/>
</StackPanel>
</Grid>
The goal is to animate the Ellipse right when hovering over the right rectangle and left when hovering over the left rectangle. What happens is that after hovering over the left one the Ellipse will no longer move right.
What's also weird is that after changing the order of the DataTrigger declarations around, the reverse is the case.
What's going on that prevents the animation from running again?
I can do this a different way using EventTriggers but in my larger scenario I am using DataTriggers and this is where I am flummoxed.
Another thing is that I wanted to Bind the DoubleAnimation.To property to the Width of the Ellipse but apparently you can't do that in Style DataTriggers and I've yet to find a good workaround. Any help is appreciated.
You need to stop the storyboard before you start other one(add names to both the storyboard & stop each of them before the other runs):
<Grid>
<Ellipse Fill="Turquoise" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100">
<Ellipse.Style>
<Style TargetType="Ellipse">
<Style.Setters>
<Setter Property="Ellipse.RenderTransform">
<Setter.Value>
<TranslateTransform X="0" Y="50"/>
</Setter.Value>
</Setter>
</Style.Setters>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=rectRight, Path=IsMouseOver}" Value="True">
<DataTrigger.EnterActions>
<StopStoryBoard BeginStoryBoardName="Second"/>
<BeginStoryboard x:Name="First">
<Storyboard TargetProperty="RenderTransform.(TranslateTransform.X)">
<!--<DoubleAnimation To="{Binding Path=Width}" Duration="0:0:1"/>--> <!--Doesn't work inside a Style Trigger-->
<DoubleAnimation To="250" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=rectLeft, Path=IsMouseOver}" Value="True">
<DataTrigger.EnterActions>
<StopStoryBoard BeginStoryBoardName="First"/>
<BeginStoryboard x:Name="Second">
<Storyboard TargetProperty="RenderTransform.(TranslateTransform.X)">
<DoubleAnimation To="45" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Bottom" Orientation="Horizontal">
<Rectangle Name="rectLeft" Fill="Gray" Height="100" Stroke="Black" Width="100" Margin="10"/>
<Rectangle Name="rectRight" Fill="Gray" Height="100" Stroke="Black" Width="100" Margin="10"/>
</StackPanel>
</Grid>

Convert a WPF Swipe Transition Datatemplate with DataTriggers to Silverlight4

I've got the following datatemplate in a WPF app I'm working on. This provides an awesome swipe transition and was originally taken from this blog post.
What I'd like to do is get the same thing working in Silverlight 4. To my horror, SL4 is missing DataTriggers and certain storyboards. Can anyone shed light on the equivalents? Alternatively, can you point to a swipe transition on the web in Silverlight I can poach? Thanks!!
The Swipe Transition
<DataTemplate x:Key="SwipeTransition">
<DataTemplate.Resources>
<Visibility x:Key="Visible">Visible</Visibility>
<Storyboard x:Key="SlideStoryboard">
<DoubleAnimation
Storyboard.TargetName="container"
Storyboard.TargetProperty="RenderTransform.(TranslateTransform.X)"
From="0" FillBehavior="Stop"
Duration="0:0:0.3"
DecelerationRatio="1.0"/>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="a"
Storyboard.TargetProperty="Visibility"
Duration="0:0:0.3" FillBehavior="Stop">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource Visible}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="b"
Storyboard.TargetProperty="Visibility"
Duration="0:0:0.3" FillBehavior="Stop">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource Visible}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</DataTemplate.Resources>
<Grid ClipToBounds="True">
<Common:Transition x:Name="transition" Source="{Binding}" />
<Grid Name="container">
<Grid.RenderTransform>
<TranslateTransform X="{Binding ElementName=container, Path=ActualWidth, Converter={StaticResource NegativeConverter}}" />
</Grid.RenderTransform>
<ContentControl Name="a" Visibility="Hidden" Content="{Binding ElementName=transition, Path=DisplayA}" />
<ContentControl Name="b" Visibility="Hidden" Content="{Binding ElementName=transition, Path=DisplayB}" />
</Grid>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=transition, Path=State}" Value="A">
<Setter TargetName="a" Property="Visibility" Value="Visible" />
<Setter TargetName="a" Property="RenderTransform">
<Setter.Value>
<TranslateTransform X="{Binding ElementName=container, Path=ActualWidth}" />
</Setter.Value>
</Setter>
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource SlideStoryboard}" />
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=transition, Path=State}" Value="B">
<Setter TargetName="b" Property="Visibility" Value="Visible" />
<Setter TargetName="b" Property="RenderTransform">
<Setter.Value>
<TranslateTransform X="{Binding ElementName=container, Path=ActualWidth}" />
</Setter.Value>
</Setter>
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource SlideStoryboard}" />
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Which is applied as follows
<ContentControl x:Name="_exampleView" Content="{Binding SelectedExample.View}"
ContentTemplate="{StaticResource SwipeTransition}"/>
first a big thanks for the link.
May be you should look at this post How to create a WPF-like data trigger in Silverlight? they stated that VSM is the way to go for silverlight.

Resources