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?
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.
WPF provides a way to inherit the style by another one. BasedOn property helps us to achieve this.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With