Event Trigger In Application Resource Breaks Designer - wpf

I added the following to a style I have defined in the Application.Resources:
<Style.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDown">
<EventTrigger.Actions>
<SoundPlayerAction Source="..\Resources\ButtonPress1.wav" />
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
It works wonderfully at run-time, however, it breaks the designer. What am I missing?
Designer Error
Thank you in advance!

He cannot find the source ..\Resources\ButtonPress1.wav

Related

Force a reload of Listbox items(usercontrols) on ListBox Visibility Changed

I have a ListBox that is bound to a List of UserControls.
The uc has a TextBlock that has an animation that fires OnLoad.
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.8" Storyboard.TargetProperty="FontSize" To="16" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style
Works fine. My problem is the List can be Collapsed by the user. When the user unCollapses the List I need the aanimation to fire again (it doesn't, of course - because they are already loaded). Doesn't seem to be any other event that would work. Soo..I'm trying to dump the ItemSource and reload them. It won't hurt as there are only up to 10 or 12 items. I have an ICommand in the ViewModel that catches the Visibility Collapse...but I'm stuck there. Thanks
What I ended up doing is copying the Collection on Collapse...then Clearing. Now I reload the copy when Visibility becomes Visible. Thanks

How to 'Freeze' the UI (main window) when mouse is hovering over

What I want to achieve is that when the mouse is hovering over the main window, all the UI elements should freeze, which I think can be done by setting Window.IsEnabled to false, and after the mouse leaves the main window, everything should be back to normal.
I've tried to define a property trigger in a style targetting Window, but it doesn't work. The code is as lollow,
<Style.Triggers>
<Trigger Property="Window.IsMouseOver" Value="True">
<Setter Property="Window.IsEnabled" Value="false"/>
</Trigger>
</Style.Triggers>
In fact this kind of property trigger wouldn't work on Grid either. Can anyone make some explanations?
I also tried to explicitly use the MouseEnter and MouseLeave events on Window, and set the disable/enable logic in the handlers. This works. I wonder if it's possible to do this in XAML?
Well to be honest I don't know why your code doesn't work, I think it goes in some kind of conflict but I don't know why
Anyway you can do it in XAML using eventsetter, It's not so elegant but it works
<Window.Triggers>
<EventTrigger RoutedEvent="Window.MouseEnter">
<BeginStoryboard>
<Storyboard Name="sb">
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" >
<BooleanKeyFrameCollection>
<DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0:1"></DiscreteBooleanKeyFrame>
</BooleanKeyFrameCollection>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>

Begin Storyboard on more than one target

I have a subclassed ListBox with a SelectedItemChanging dependency property that is set to a Storyboard. When the selected item is changed, I want to run this Storyboard on each item in the ListBox.
How is this possible with a single instance of Storyboard?
Storyboards can be keyed and run from multiple triggers, and it works great as long as it's set up properly. If I am understanding you correctly, you're hoping to apply the storyboard to each individual ListBoxItem. In which case, why not make a style, and on that style's triggers, fire the storyboard.
Excuse my pseudocode.
<Storyboard x:Key="MyEnterStoryboard">
<!-- Do Enter Work -->
</Storyboard>
<Storyboard x:Key="MyExitStoryboard">
<!-- Do Exit Work -->
</Storyboard>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="SelectedItemChanging" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MyEnterStoryboard}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource MyExitStoryboard}"/>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
WPF Storyboards have a Clone method. Silverlight doesn't have this but thought I'd post it just in case someone stumbles across this post looking for a WPF solution.

Error when using StaticResource

I'm getting this error
Attribute {StaticResource
StoryboardIntroAnimation} value is out
of range
when I try and use a staic resource as the Storyboard property of a BeginStoryboard object. The markup looks a little like this:
<UserControl ...>
<UserControl.Resources>
<Storyboard x:Key="StoryboardIntroAnimation">
...
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger>
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{StaticResource StoryboardIntroAnimation}" />
</EventTrigger.Actions>
</EventTrigger>
</UserControl.Triggers>
...
</UserControl>
Does anyone know why this is happening?
Set RoutedEvent on your EventTrigger ?

In WPF, is it possible to specify multiple routed events for a single event trigger?

I have an event trigger that I want to be fired in response to two different routed events. I don't want to repeat the event response code (in XAML) twice. Can I specify multiple routed events for a single event trigger declaration?
Example of a single event:
<Style.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<--XAML MAGIC-->
...
Sorry... WPF's implementation of the EventTrigger only allows one routed event.
Typically you would be firing a storyboard from a routed event. I would suggest the following compromise:
<!--Define a storyboard as a resource-->
<Storyboard x:Key="MyStoryboard1">
<!--Many properties and etc...-->
</Storyboard>
<Style.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard Storyboard="{StaticResource MyStoryboard1}">
<!--Other properties/name if necessary-->
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Button.MouseDown">
<BeginStoryboard Storyboard="{StaticResource MyStoryboard1}">
<!--Other properties/name if necessary-->
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
The concept is to reduce duplicate code by sharing resources.
Hope this helps!

Resources