Reputation: 1244
I am building a WPF application and have set the window background to a "Wheat" but when I run the application the background is black. Can anyone shed some light on what's going on?
Design Time
Run Time
Here is the XAML:
<Window x:Class="App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="350" Width="525"
TextElement.FontFamily="Calibri"
TextElement.FontSize="14"
Background="Wheat">
<Window.Resources>
<XmlDataProvider x:Key="dbInfo" Source="DBConnectionInfo.xml" XPath="dbConnectionInfo"/>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="CornerRadius" Value="4"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="StackPanel" x:Key="mainStackPanel">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="10"/>
</Style>
<Style TargetType="StackPanel">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="10"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Margin" Value="10,10,10,10"/>
</Style>
<Style TargetType="ComboBox">
<Setter Property="Margin" Value="10,10,10,10"/>
</Style>
<Style TargetType="Button">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Padding" Value="10,5,10,5"/>
<Setter Property="Margin" Value="5"/>
</Style>
</Window.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0" Style="{StaticResource mainStackPanel}">
<Grid Visibility="Visible" Name="loginGrid">
<Border>
<StackPanel>
<TextBlock>Database Server / Listener</TextBlock>
<ComboBox Name="hostComboBox"
ItemsSource="{Binding Source={StaticResource dbInfo},
XPath=./dbConnections/dbConnection}"
DisplayMemberPath="@database"
SelectedValuePath="@host"
SelectedIndex="1"/>
<Button Name="loginButton" Click="loginButton_Click" Content="Open"/>
</StackPanel>
</Border>
</Grid>
<Grid Visibility="Collapsed" Name="selectGrid">
<Border>
<StackPanel Grid.Column="0" Grid.Row="0" Orientation="Vertical" Background="Transparent">
<Button Content="Forms"/>
<Button Content="Documents"/>
</StackPanel>
</Border>
</Grid>
</StackPanel>
</Grid>
</Window>
Upvotes: 2
Views: 1753
Reputation: 11635
The problematic code was the Horizontal
and Vertical
alignment of the Style for the Border. When no Key is specified it is used as the default style for the TargetType, thus it will apply to all Borders in your project. This will give the same result as your code:
<Window 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"
mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Background="Red">
<Window.Resources>
<Style TargetType="Border">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</Window.Resources>
</Window>
The problem is, the style for the Window uses Borders for its Control Template. Using Blend, you can edit a copy of the applied Template, which will give you:
<ControlTemplate TargetType="{x:Type Window}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
</ControlTemplate>
The border for the Template will pick up your style and apply it to its internal Border
. So, to resolve your issue, either apply a ControlTemplate for your Window where you set the Horizontal and Vertical Alignment:
<Window.Template>
<ControlTemplate TargetType="{x:Type Window}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
</ControlTemplate>
</Window.Template>
or apply a x:Key
to your border style and add that to the borders to which you want to use that style
<Style x:Key="MyBorderStyle" TargetType="Border">
<Border Style="{StaticResource MyBorderStyle}">
</Border>
Upvotes: 1