Reputation:
Following xaml code works good in the Window.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard Duration="00:00:2" Storyboard.TargetProperty="Opacity">
<DoubleAnimation From="0" To="1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
I have five WPF Windows in the my WPF Application.
I dont want to put above code into every WPF Windows.
So, how to put above code into Application.Resources in order to work for 5 windows?
Upvotes: 1
Views: 51
Reputation: 13448
First, lets develop a suitable Style
from your triggers:
<Style TargetType="{x:Type Window}">
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard Duration="00:00:2" Storyboard.TargetProperty="Opacity">
<DoubleAnimation From="0" To="1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
This style can be placed in application resources.
Now the problem is, your window doesn't consume the default style from resource, so it needs to be explicitely applied (See How to set default WPF Window Style in app.xaml? for more details)
<Window ... Style="{StaticResource {x:Type Window}}">
Upvotes: 3