Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does style TargetType="Window" not work when set from App.xaml?

Tags:

c#

window

wpf

xaml

I'm creating a simple WPF project in VS2013 and I want to apply properties to my main Window. I set them in my App.xaml file like this:

<Application.Resources>
    <Style TargetType="Window">
        <Setter Property="Background" Value="#FF2D2D30" />
    </Style>
</Application.Resources>

The problem is that nothing happens. When I change the TargetType to Grid however, the setter property works just fine. Why does this happen?

like image 523
Audio Avatar asked Mar 08 '14 13:03

Audio


People also ask

How do you use style in XAML?

The most common way to declare a style is as a resource in the Resources section in a XAML file. Because styles are resources, they obey the same scoping rules that apply to all resources. Put simply, where you declare a style affects where the style can be applied.

Can style be inherited or extended in WPF?

WPF provides a way to inherit the style by another one. BasedOn property helps us to achieve this.

What is style in XAML?

You can customize the appearance of your apps in many ways by using the XAML framework. Styles let you set control properties and reuse those settings for a consistent appearance across multiple controls.

What is app XAML used for?

In a Xamarin. Forms application, XAML is mostly used to define the visual contents of a page and works together with a C# code-behind file. The code-behind file provides code support for the markup. Together, these two files contribute to a new class definition that includes child views and property initialization.


1 Answers

It is necessary to add construction in Window:

Style="{StaticResource {x:Type Window}}"

Window in XAML:

<Window x:Class="WindowStyleHelp.MainWindow"
        Style="{StaticResource {x:Type Window}}"
        ...>

Or define Style in resources like this:

xmlns:local="clr-namespace:MyWpfApplication"

<Application.Resources>
    <Style TargetType="{x:Type local:MainWindow}">
        <Setter Property="Background" Value="#FF2D2D30"/>
    </Style>
</Application.Resources>
like image 161
Anatoliy Nikolaev Avatar answered Oct 13 '22 10:10

Anatoliy Nikolaev