Event for MouseOver action in WPF - wpf

I want to handle mouse over and mouse out events for a grid. Does WPF have events for this.
Note: I dont want to use IsMouseOver property in my style.
i have used MouseEnter and MouseLeave method but without much success.

You can use EventTriggers to capture MouseEnter and MouseLeave events in XAML.
Here is a simple example that changes the background of a StackPanel in a Grid:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Background="Blue">
<StackPanel.Style>
<Style>
<Style.Triggers>
<EventTrigger RoutedEvent="StackPanel.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
AutoReverse="False"
Duration="0:0:1"
From="Blue" To="Red"
AccelerationRatio="1"
Storyboard.TargetProperty="(StackPanel.Background).(SolidColorBrush.Color)"
FillBehavior="HoldEnd">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="StackPanel.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
AutoReverse="False"
Duration="0:0:1"
From="Red" To="Blue"
AccelerationRatio="1"
Storyboard.TargetProperty="(StackPanel.Background).(SolidColorBrush.Color)"
FillBehavior="HoldEnd">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
</Grid>

A WPF Grid control supports both the MouseEnter and MouseLeave events. You should be able to hook up event handlers for both.

More simple :
You can implement the two events PointerMoved and PointerExited. It worked for me.

MouseEnter and MouseLeave events may be handled , you can check your code set e.handled = false;

Related

Add Color Animation to Mah:Tile

I'm Using MahApps.Metro and I'm using a Tile Element which Is loaded with namespace 'MahCtrl'( xmlns:MahCtrl="http://metro.mahapps.com/winfx/xaml/controls). I want to apply ColorAnimation to the Tile on MouseEnter and MouseLeave.
Here is the xaml snippet I am Currently working on.
<UserControl xmlns:MahCtrl="http://metro.mahapps.com/winfx/xaml/controls">
<MahCtrl:Tile Cursor="Hand" Background="Transparent" Height="200" Width="210"HorizontalContentAlignment="Center">
<MahCtrl:Tile.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Duration="0:0:0.200"
Storyboard.TargetProperty="(MahCtrl:Tile.Background).Color"
To="#fffccc" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Duration="0:0:0.250"
Storyboard.TargetProperty="(MahCtrl:Tile.Background).Color"
To="#ffffff" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</MahCtrl:Tile.Triggers>
</MahCtrl:Tile>
</UserControl>
In the debug section, When I Enter mouse into the Tile the following exception occurs
System.InvalidOperationException: 'Cannot resolve all property
references in the property path '(0).Color'. Verify that applicable
objects support the properties.'
I have tried Using (Background).Color and a lot of other combinations to StoryBoard.TargetProperty But this method works when MahCtrl:Tile element is wrapped with a stack panel and applying event triggers MouseEnter and MouseLeave triggers with target (StackPanel.Background).Color to the StackPanel. How can I target background property of MahCtrl:Tile
It would be a great help if someone can refer to a documentation regarding this topic
Thank you very much,

Change MouseLeave animation when Grid is clicked

In my WPF appliction, I have a partially visible grid at the bottom of the visible space. When you mouseover it, it slides up and everything in the grid is visible. When the mouse leaves the grid, it slides back down. There are Combo and TextBoxes in the grid that will contain info and be able to be updated by the user.
How do I make it so that if I click anywhere in the grid, the MouseLeave animation no longer goes off (the grid stays up even you if your mouse leaves)?
I have implemented my animation 2 different ways in an attempt to get it to behave the way I want, but no luck so far.
Implementation 1:
`<Grid x:Name="PopupAnimation" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="30">
<Grid.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard Name="MouseOn">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height" From="30" To="90" Duration="0:0:.15"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard Name="MouseOff">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height" From="90" To="30" Duration="0:0:0.15"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeftButtonDown">
<StopStoryboard BeginStoryboardName="MouseOff" />
</EventTrigger>
</Grid.Triggers>
`
Implementation 2:
<Grid>
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Name="MouseOn">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height" From="30" To="90" Duration="0:0:.15"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Name="MouseOff">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height" From="90" To="30" Duration="0:0:0.15"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
Are either of these heading down the right road? How do I disable (and enable) an animation?
How do I make it so that if I click anywhere in the grid, the MouseLeave animation no longer goes off (the grid stays up even you if your mouse leaves)?
Don't execute the up and down movement by style, instead subscribe to the all events in question, and with the events of mouse enter and mouse leave run the storyboards in code-behind.
Then in the final event, in code behind grid click event, set a Boolean flag which will be read by the mouse leave event and allow it to not execute the storyboard.
Here is code I have to move a window in/out in codebehind to give you an idea about the storyboards:
if (moveRight)
{
(Resources["MoveToOpen"] as Storyboard)?.Begin(this, false);
(Resources["FlipArrowClose"] as Storyboard)?.Begin(this, false);
}
else
{
if (closeWindow == false)
{
(Resources["MoveToClose"] as Storyboard)?.Begin(this, false);
(Resources["FlipArrowOpen"] as Storyboard)?.Begin(this, false);
}
}

Button appearance animation in Datatemplate

I have a button inside a DataTemplate of a ListBox.
Whenever a button is added to the Listbox,I want to animate the appearance of the new button by setting the opacity from minimum to maximum.
How do I do this?
You could listen to Loaded event. When Loaded wpf will trigger your animation.
Here is an example how you could achieve that:
<Button Height="23" Margin="102,95,100,0" Name="button3" VerticalAlignment="Top" Content="Opacity">
<Button.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="1"
To="0"
Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>

Silverlight 3: Event trigger argues that attribute is out of range

I want to make a grid non-transparent when mouse enters. So I try to catch MouseEnter event and animate Opactity property
<Grid Opacity="0.1" Name="myGrid" >
<Grid.Triggers>
<EventTrigger RoutedEvent="Grid.MouseEnter" >
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="0.1" To="1" Duration="00:00:01" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="myGrid"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
</Grid>
But InitializeComponent argues that Grid.MouseEnter attribute is out of range. What I am doing wrong?
Your code worked here.
I use Visual Studio 2010 and pasted your code in to a C# WPF Application.
You need to specify the event as MouseEnter instead of Grid.MouseEnter

wpf animation events overlapping

I've got a canvas control that grows in height when you move the mouse over it and shrinks back on mouse leave.
<Canvas x:Name="infoBar" Width="720" Height="39" Background="Red">
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="infoBar"
Storyboard.TargetProperty="Height"
From="39" To="255" Duration="0:0:0.5"
/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Canvas.MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="infoBar"
Storyboard.TargetProperty="Height"
From="255" To="39" Duration="0:0:0.5"
/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>
<StackPanel>
<TextBlock/>
<TextBlock/>
</StackPanel>
</Canvas>
This works fine. However if two quick consecutive events take place (mouseleave before mouse enter animation finishes) it goes nuts.
Is there anyway i can tell it to cancel out any other events that happen before an animation finishes?
Using your event triggers you can perform pause, stop, resume, etc. commands on named storyboards.
This article should answer your questions.

Resources