Notts90
Notts90

Reputation: 255

Unexpected style effect on windows' border

The MainWindow in my project has a lot of controls that have borders so I created a style in Window.Resources that would apply to every border in the window. In it I set that margin to be 5 and something unexpected happened.

It seems it also applies it to the border of the actual window resulting in a black outline in my window.

To test this I created a new project and added this code and got the same result. What appears even odder is it only seems to be the margin that it uses. Not border thickness or colour.

<Window.Resources>
    <Style TargetType="{X:Type Border}">
        <Setter Property="BorderBrush" Value="Aqua"/>
        <Setter Property="BorderThickness" Value="5"/>
        <Setter Property="Margin" Value="5"/>
    </Style>
</Window.Resources>

The question is, how can I set the margin on this border to over ride the style?

I know I could add a key to the style but I then have to go edit all the borders to use this style. It's seems a bit wrong that I can't easily set the margin on this specific border.

Any help/guidance appreciated.

P.S. Apologies for any typos or formatting issues, types this all on my phone as not got internet access on coding laptop.

Upvotes: 0

Views: 54

Answers (1)

Funk
Funk

Reputation: 11221

Try moving the Style from Window to MainContainer Resources

<Window
    ...
    >
    <Grid x:Name="MainContainer">
        <Grid.Resources>
            <Style TargetType="{x:Type Border}">
                <Setter Property="BorderBrush" Value="Aqua"/>
                <Setter Property="BorderThickness" Value="5"/>
                <Setter Property="Margin" Value="5"/>
            </Style>
        </Grid.Resources>
        ...
    </Grid>
</Window>

Upvotes: 1

Related Questions