Alex Hope O'Connor
Alex Hope O'Connor

Reputation: 9714

Styles defined in Application.Resources not applying to controls

I am trying to apply global application styles to certain control types, however adding these styles to Application.Resources does is not applying the styles to the elements in my views.

Example:

<Application x:Class="GUI.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Application.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Background" Value="AliceBlue"></Setter>
            <Setter Property="Margin" Value="20,20,20,20"></Setter>
            <Setter Property="FontStyle" Value="Italic"></Setter>
        </Style>
    </Application.Resources>
</Application>

In all the examples I have found for applying application wide styles this has been how they say to do it, however it is not working for me. What am I doing wrong?

Thanks, Alex.

Upvotes: 2

Views: 1493

Answers (2)

Ryan Thiele
Ryan Thiele

Reputation: 364

Eventhough this is an old post and has been answered. I came across this problem. I removed StartupUri and added and empty style (I used the question as an example):

<Application x:Class="GUI.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Application.Resources>
        <!-- Added blank style first -->
        <Style TargetType="Rectangle" />
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Background" Value="AliceBlue"></Setter>
            <Setter Property="Margin" Value="20,20,20,20"></Setter>
            <Setter Property="FontStyle" Value="Italic"></Setter>
        </Style>
    </Application.Resources>
</Application>

Upvotes: 1

Alex Hope O&#39;Connor
Alex Hope O&#39;Connor

Reputation: 9714

Worked this out myself woops, the problem is I was not using the StartUpUri property to open my initial application view, I changed my start up process so it does use this property and this has fixed my problem.

My App.xaml now looks like this:

<Application x:Class="GUI.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="/Views/Application/SplashView.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Background" Value="Aqua"></Setter>
        </Style>
    </Application.Resources>
</Application>

Thanks, Alex.

Upvotes: 2

Related Questions