user7747311
user7747311

Reputation:

How to move XAML codes from Window to Application.Resources

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

Answers (1)

grek40
grek40

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

Related Questions