Reputation: 87
I have a simple WPF (4.0) sample that aims the demonstrate the strangeness I am facing in another project. The sample tries to display a few rectangle shapes for which I use Border elements. Below is the markup:
<Window x:Class="WpfApplication1.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="1000" Width="1500">
<Window.Resources>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" x:Key="grad">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
<Style TargetType="Border">
<Setter Property="Background" Value="{StaticResource grad}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="100" />
<Setter Property="BorderThickness" Value="1" />
</Style>
</Window.Resources>
<StackPanel Orientation="Horizontal">
<Border />
<Border />
<Border />
</StackPanel>
</Window>
In the designer this looks exactly how I imagine it: 3 squares next to each other on the top left of the screen. However at runtime only 1 is displayed and the rest of the window is black. Seemingly the window's content itself is affected by the layout properties. If I use Rectangles instead of Borders the sample works just fine. Can anyone explain please why I am seeing this behaviour (inheritance related maybe)? Also is the difference between design-time view and run-time view basically a VS bug?
Upvotes: 1
Views: 1567
Reputation: 32481
Change it like this:
<Style TargetType="Border" x:Key="myborder">
<Setter Property="Background" Value="{StaticResource grad}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="100" />
<Setter Property="BorderThickness" Value="1" />
</Style>
</Window.Resources>
<StackPanel Orientation="Horizontal">
<Border Style = {StaticResources myborder}/>
<Border Style = {StaticResources myborder}/>
<Border Style = {StaticResources myborder}/>
</StackPanel>
Now its OK.
EDIT:
Each Winwow's template is a Border that has a AdornerDecorator in it. And in that decorator there is a ContentPresenter
. So when you create a default style for borders it affect the Window's border too.
Upvotes: 1