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

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

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,

Two way animation

I'm triying to hide/show a stackpanel when I click in a button. This is what I made so far:
<Button.Triggers>
<EventTrigger RoutedEvent="Mouse.PreviewMouseDown">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height"
Storyboard.TargetName="PanelDeCampos"
From="{Binding ElementName=PanelDeCampos,Path=ActualHeight}"
To="0"
Duration="0:0:0.25" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
This animation works well, and it hides the panel when I click on it. But now I need to find a way to launch the reverse animation when the button is clicked again. Could I store the current state and decide what animation launch or something like this?
Thanks.
You can change your button to a ToggleButton and use the Checked and Unchecked routed events to set up your 2 storyboards:
<ToggleButton>
<ToggleButton.Triggers>
<EventTrigger RoutedEvent="Checked">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height"
Storyboard.TargetName="PanelDeCampos"
From="{Binding ElementName=PanelDeCampos,Path=ActualHeight}"
To="0"
Duration="0:0:0.25" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Unchecked">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Height"
Storyboard.TargetName="PanelDeCampos"
From="0"
Duration="0:0:0.25"
To="1000" /> <!-- or whatever height you want-->
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ToggleButton.Triggers>
</ToggleButton>
What about a little trick:
Together with the storyboard you have in place now, you are hidding the current button and setting another button (which looks the same) visible.
The other button has the reverse storyboard, and reverse button visibility settings.
With that you don't have to worry about a state, and you can do it only in XAML.
Other Idea would be to handle the click in the code behind, maintain a flag there, and trigger the storyboard from the code. As this is a view-only functionality I don't see a conflict with MVVM.

Silverlight - how to set trigger to popup

I have a popup with UserControl inside. I need to hide this popup and want to create it by trigger. I try the following code:
<Popup.Triggers>
<EventTrigger RoutedEvent="Popup.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetName="popup"
Storyboard.TargetProperty="Popup.IsOpen"
To="False" From="True" Duration="0:0:2">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Popup.Triggers>
<blib:TimeZones></blib:TimeZones>
</Popup>
but it does not work
Failed to assign to property
'System.Windows.EventTrigger.RoutedEvent'. [Line: 55 Position: 47]
How to make it correctly?
Triggers in Silverlight don't work in the same way as WPF triggers do. You need to use the interactivity library. Check out this article: http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx

Event for MouseOver action in 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;

WPF Focus Animation not working properly

I have a WPF usercontrol set up in XAML with the following Triggers:
<UserControl.Triggers>
<EventTrigger RoutedEvent="GotFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="1.5" Storyboard.TargetName="ImageContent" Storyboard.TargetProperty="RenderTransform.Children[1].ScaleX" Duration="0:0:0.8" />
<DoubleAnimation To="1.5" Storyboard.TargetName="ImageContent" Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" Duration="0:0:0.8" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="LostFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="1" Storyboard.TargetName="ImageContent" Storyboard.TargetProperty="RenderTransform.Children[1].ScaleX" Duration="0:0:0.8" />
<DoubleAnimation To="1" Storyboard.TargetName="ImageContent" Storyboard.TargetProperty="RenderTransform.Children[1].ScaleY" Duration="0:0:0.8" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</UserControl.Triggers>
The Animation is working fine but unfortunately the Triggers arent working as expected. The GotFocus trigger only fires if i Right-Click my Control or if i Click a Button that is contained within the UserControl.
I have created an EventHandler for the GotFocus event in code and that seems to fire in the right places.
Edit: If I change it to Fire on MouseEnter / Leave it also works as expected
Edit2: I foudn out that this behaviour is surfacing because the Control gets focus, but loses the focus again right away. I fixed it by adding some code, that checks if the source of the Event is the UserControl or something else.
Any hints on why this is happening would be appreciated.
Try settings Focusable="True" on the UserControl.

Resources