Hi Everyone,

I started creating a new WPF project wherein, I have just added a style to all the elements of type "Window" and "Grid" but none of these are working (rather a wierd black box is apprearing - Screenshot attached).
NOTE: I do not want to use "x:Key" for the styles as I want these styles to be automatically applied to all the windows which I would add further.

Here is the code below:

Window XAML:
<Window x:Class="General_WPF.Views.StartPage_View"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:General_WPF.Views"
        xmlns:viewModel="clr-namespace:General_WPF.ViewModels"
        mc:Ignorable="d" Title="Welcome User">
    <Window.DataContext>
        <viewModel:StartPage_VM />
    </Window.DataContext>

    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="Hello" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        <TextBox Grid.Row="1" Height="25" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</Window>
Style:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:General_WPF.ResourceDictionaries">

    <Style TargetType="{x:Type Window}">
        <Setter Property="Height" Value="500" />
        <Setter Property="Width" Value="500" />
        <Setter Property="Background" Value="Red" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="AllowsTransparency" Value="True"/>
    </Style>

    <Style TargetType="{x:Type Grid}">
        <Setter Property="Height" Value="250" />
        <Setter Property="Width" Value="250" />
        <Setter Property="Background" Value="LightSalmon" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>  

</ResourceDictionary>
App.xaml:
<Application x:Class="General_WPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:General_WPF"
             StartupUri="Views/StartPage_View.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ResourceDictionaries\MyStyle.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

WpfStyleFile.PNG
I've tried your code and the only way I could change the window's background was setting x:Key.

MainWindow.xaml:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Style="{StaticResource MyWindowStyle}"  >
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="Hello" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        <TextBox Grid.Row="1" Height="25" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</Window>

myStyle.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="MyWindowStyle">
        <Setter Property="Control.Height" Value="500" />
        <Setter Property="Control.Width" Value="500" />
        <Setter Property="Control.Background" >
            <Setter.Value>
                <SolidColorBrush Color="Red" Opacity="0.2" />
            </Setter.Value>
        </Setter>
        <Setter Property="Window.Title" Value="Styled Window"/>
        <Setter Property="Control.HorizontalAlignment" Value="Center" />
        <Setter Property="Control.VerticalAlignment" Value="Center" />
        <Setter Property="Window.WindowStyle" Value="None"/>
        <Setter Property="Window.AllowsTransparency" Value="True"/>
    </Style>
    <Style TargetType="{x:Type Grid}">
        <Setter Property="Height" Value="250" />
        <Setter Property="Width" Value="250" />
        <Setter Property="Background" Value="LightSalmon" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>
</ResourceDictionary>

Application.xaml:

<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml" >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="myStyle.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</Application.Resources>
</Application>

I was refreshing my reading on styles and this passage kept coming up. "apply styling to your controls" as in the style is all about the controls and not the window itself.

What are you reading?

Hi xrj, I know that using the x:key would surely solve this problem but this would not be a generic solution as I would need to apply this key to every window's style which I don't want to.

Hi rproffitt, probably you're new to WPF (first of all consider reading and rather implementing other books too) as this is a basic thing about applying styles to the controls (that includes Window itself too). Let me tell you a basic thing. All the elements are derived from "FrameworkElement" class whether it is a Window or any control because Window is itself a control. Also, to your first reply, I already mentioned that I want to make this approach generic and by using keys, I would need to manually apply that style in all the windows which is NOT a general approach (I am clearly usiung only "x:Type") (Please read first before posting, as I already saw your replies to my previous post).
Also, rproffitt, do not consider this as a rude response because this is exactly like what you've posted inside my previous article (I believe just to earn reputation points or increase your posting score).

commented: Points can go either way. Chill. +12

Additionally supressing Style="{StaticResource MyWindowStyle}" you may add this event in code behind MainWindow:

    Private Sub MainWindow_Initialized(sender As System.Object, e As System.EventArgs)
        Me.Style = FindResource("MyWindowStyle")
    End Sub

I've search a quite and what you're looking for seems quite sure to not work. Another work around is to add a 'myWindow' class inherited from window and set the Initialized event as I did before. In this way you won't have to add the Style attribute, but you'll need to derive the MainWindow, as any other window, from 'myWindow'.

@KM not at all rude. I do have apps in C# and found that styles only appled to just a few things but not the window. So as xrj writes about adding a myWindow class to make an app wide style, it's not styles in the strict sense.

As to points, have a few in kind?

commented: Thanks, I'll surely consider this +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.