wpf eventtrigger not triggering
- Hi,
I have a window which has ellipses and i need to animate it.
see the code.
<
Window x:Class="TM.Kiosk.Client.ProgressDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="100" WindowStartupLocation="CenterScreen" WindowStyle="None">
<Border BorderBrush="Black" BorderThickness="2" Background="LightGray">
<Canvas Background="LightGray">
<Canvas.Resources>
<ParallelTimeline x:Key="fade">
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.Opacity)" From="1" To="0"
Duration="0:0:0.1" AutoReverse="True"/>
</ParallelTimeline>
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Ellipse.Width" Value="3" />
<Setter Property="Ellipse.Height" Value="3" />
<Setter Property="Fill" Value="Black"/>
</Style>
</Canvas.Resources>
<Canvas.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard Duration="0:0:1.2" RepeatBehavior="Forever">
<Storyboard TargetName="ellip1" BeginTime="0:0:0.0">
<StaticResource ResourceKey="fade" />
</Storyboard>
when i run this window directly it is working fine.but when i show this window from some other window(click of a button on another window) the ellipses are not moving.
i think it is not trigerring the event.is there any way to trigger this from code behind?
Toutes les réponses
- Hi sa123,
it'd sure help if you posted the window's entire XAML. FWIW, why don't you set the trigger to Window.Loaded ..? Actually, both should work. That said, do you actually close the window before showing it again? If not, you might want to base your trigger on the window's Visibility instead.
Cheers,
Olaf- ModifiéOlaf Rabbachin jeudi 5 novembre 2009 10:00
- Hi,
Thanks for the reply.
See the entire code below.
<
Window x:Class="TM.Kiosk.Client.ProgressDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="100" WindowStartupLocation="CenterScreen" WindowStyle="None">
<Window.Resources>
<ParallelTimeline x:Key="fade">
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.Opacity)" From="1" To="0"
Duration="0:0:0.1" AutoReverse="True"/>
</ParallelTimeline>
<Style TargetType="{x:Type Ellipse}">
<Setter Property="Ellipse.Width" Value="3" />
<Setter Property="Ellipse.Height" Value="3" />
<Setter Property="Fill" Value="Black"/>
</Style>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard Duration="0:0:1.2" RepeatBehavior="Forever">
<Storyboard TargetName="ellip1" BeginTime="0:0:0.0">
<StaticResource ResourceKey="fade" />
</Storyboard>
<Storyboard TargetName="ellip2" BeginTime="0:0:0.1">
<StaticResource ResourceKey="fade" />
</Storyboard>
<Storyboard TargetName="ellip3" BeginTime="0:0:0.2">
<StaticResource ResourceKey="fade" />
</Storyboard>
<Storyboard TargetName="ellip4" BeginTime="0:0:0.3">
<StaticResource ResourceKey="fade" />
</Storyboard>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Window.Triggers>
<Border BorderBrush="Black" BorderThickness="2" Background="LightGray">
<Canvas Background="LightGray">
<TextBlock Text="Please wait" x:Name="txtDialogMessage1" Canvas.Left="100" Canvas.Top="38"Height="25" Foreground="Black" FontSize="14"/>
<Ellipse x:Name="ellip1" Canvas.Left="171" Canvas.Top="49"></Ellipse>
<Ellipse x:Name="ellip2" Canvas.Left="176" Canvas.Top="49"></Ellipse>
<Ellipse x:Name="ellip3" Canvas.Left="181" Canvas.Top="49"></Ellipse>
<Ellipse x:Name="ellip4" Canvas.Left="187" Canvas.Top="49"></Ellipse>
</Canvas>
</Border>
</
Window>
The above window will be called from some other window say master on the click of next button.
dlg.Owner =
Me
dlg.WindowStartupLocation = WindowStartupLocation.CenterOwner
btnSecond.IsEnabled =
False
Master.Opacity = 0.4
dlg.Visibility = Windows.Visibility.Visible
Am just setting the visibilty to hidden and visible based on the requirement.
<EventTrigger RoutedEvent="Window.Loaded"> is not working as pointed out by you.
<EventTrigger RoutedEvent="Window.Visibility"> showing error no such event name.
Please advice. - Hi sa123,
Window.Visibility is not routed event, but property name. I have performed a test, and find no problem with the code. The ellipses animating perfectly when I open the loading dialog from the button in another window.
Not sure how you show the loading dialog, I use ShowDialog method as the following:
Let me know if I was doing something different.private void Button_Click(object sender, RoutedEventArgs e) { Window1 dlg = new Window1(); dlg.ShowDialog(); dlg.Owner = this; dlg.WindowStartupLocation = WindowStartupLocation.CenterOwner; <br/> }
Best regards,
Bruce Zhou
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. - Hi sa123,
a trigger for Window.Loaded will only work once, i.e. when the window is loaded. Since you seem to be, instead of actually closing and reopening the window, only hiding it, the trigger will not fire as the window is never loaded again.
Regarding a trigger for visibility, I haven't found anything which could be of use for that either. However, you can start and stop your storyboard from code when the IsVisibleChanged event is fired on the window level. This requires a couple of changes to your XAML:
<Window x:Class="WpfTests.EllipseAnimation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="100" WindowStartupLocation="CenterScreen" WindowStyle="None"> <Window.Resources> <ParallelTimeline x:Key="fade"> <DoubleAnimation Storyboard.TargetProperty="(Ellipse.Opacity)" From="1" To="0" Duration="0:0:0.1" AutoReverse="True"/> </ParallelTimeline> <Style TargetType="{x:Type Ellipse}"> <Setter Property="Ellipse.Width" Value="3" /> <Setter Property="Ellipse.Height" Value="3" /> <Setter Property="Fill" Value="Black"/> </Style> <Storyboard x:Key="FaderStoryboard" Duration="0:0:1.2" RepeatBehavior="Forever"> <Storyboard TargetName="ellip1" BeginTime="0:0:0.0"> <StaticResource ResourceKey="fade" /> </Storyboard> <Storyboard TargetName="ellip2" BeginTime="0:0:0.1"> <StaticResource ResourceKey="fade" /> </Storyboard> <Storyboard TargetName="ellip3" BeginTime="0:0:0.2"> <StaticResource ResourceKey="fade" /> </Storyboard> <Storyboard TargetName="ellip4" BeginTime="0:0:0.3"> <StaticResource ResourceKey="fade" /> </Storyboard> </Storyboard> </Window.Resources> <Border BorderBrush="Black" BorderThickness="2" Background="LightGray"> <Canvas Background="LightGray"> <TextBlock Text="Please wait" x:Name="txtDialogMessage1" Canvas.Left="100" Canvas.Top="38" Height="25" Foreground="Black" FontSize="14"/> <Ellipse x:Name="ellip1" Canvas.Left="171" Canvas.Top="49"></Ellipse> <Ellipse x:Name="ellip2" Canvas.Left="176" Canvas.Top="49"></Ellipse> <Ellipse x:Name="ellip3" Canvas.Left="181" Canvas.Top="49"></Ellipse> <Ellipse x:Name="ellip4" Canvas.Left="187" Canvas.Top="49"></Ellipse> </Canvas> </Border> </Window>
Now, the storyboard is made available as a resource that you can use from your code. Here's the code-behind:
using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; namespace WpfTests { /// <summary> /// Interaction logic for EllipseAnimation.xaml /// </summary> public partial class EllipseAnimation : Window { public EllipseAnimation() { InitializeComponent(); this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(EllipseAnimation_IsVisibleChanged); } void EllipseAnimation_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { Storyboard sb = (Storyboard)this.FindResource("FaderStoryboard"); if (this.Visibility == Visibility.Visible) sb.Begin(); else sb.Stop(); } } }
As you can see here, the storyboard is started when the window gets visible and stopped otherwise.
Cheers,
Olaf - Hi Bruce,
dlg.ShowDialog() will definitely work but i cannot continue exceution till i close the window.So the above way cannot be implemented because i need to show the dialog at the same time using dispatcherqueue i have to execute another process.
Thanks for the reply. - Hi Olaf,
Even after implementing your code am not getting the animation.It is just showing the ellipses.
If i use showdialog it it working.But i cannot use that. - Hi sa123,
If I use Show method instead of ShowDialog, the animation still works. If you need my test solution, I would love to share it.
Best regards,
Bruce Zhou
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. - Hi sa123,
not sure as to why you should be having problems with that. For my tests, I simply added a x:name-prop to the window and another window that lets me toggle the visibility of the wait-window. No problems here ...
Cheers,
Olaf - Hi Bruce,
Please share the test solution.I want to check why it is not working here.
Thanks,
Savitha - Hi Olaf,
If you dont mind could you please provide the test solution. - Hi sa123,
If you dont mind could you please provide the test solution.
you can download it here .
Cheers,
Olaf - <bump>
Did you have a look at the test-solution I provided ..?
Cheers,
Olaf

