1

I am working on a WPF application where I'm trying to define different common styles to be used throughout the application. I'm quite new in this area, so I don't know whether there is something I've misunderstood. Anyway, here it goes:

As an example, I have created a Resource Dictionary with different common formattings. One of these is a format for a Border. The definition goes like this:

<Style x:Key="MainBorderStyle" TargetType="{x:Type Border}">
    <Setter Property="IsEnabled"
            Value="True" />
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="BorderBrush"
            Value="Gold" />
    <Setter Property="CornerRadius"
            Value="6" />
    <Setter Property="BorderThickness"
            Value="2" />
    <Setter Property="Padding"
            Value="2" />
</Style>

Now, I want to use this formatting throughout the application by different elements - buttons, rectangles, group boxes, etc.

At the moment I have troubles applying the border format to GroupBox controls. I also have a predefined format for GroupBox controls.

In several places in the XAML code, I have had luck applying a reference to the Border setting above, by using the code snippet

<Border Style="{StaticResource MainBorderStyle}" x:Key="SomeKeyID" />

However, for GroupBoxes for example, I can't make this working, and I have to provide all the formatting for the brushes, corners, etc. repeatedly within each groupbox.

What can the problem be? Any suggestions will be highly appreciated.

Best regards.

1 Answer 1

2

Style targeting Border cannot be applied to elements of a different type, like GroupBox.

you can create a new MainGroupBoxStyle, copy setters from MainBorderStyle and apply it to GroupBoxes:

<Style x:Key="MainGroupBoxStyle" TargetType="{x:Type GroupBox}">
    <Setter Property="IsEnabled"
            Value="True" />
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="BorderBrush"
            Value="Gold" />
    <Setter Property="BorderThickness"
            Value="2" />
    <Setter Property="Padding"
            Value="2" />
</Style>

or use a Style without TargetType:

<Window.Resources>
    <Style x:Key="MainBorderStyle">
        <Setter Property="UIElement.IsEnabled"
                Value="True" />
        <Setter Property="Panel.Background"
                Value="Transparent" />
        <Setter Property="Border.BorderBrush"
                Value="Gold" />
        <Setter Property="Border.CornerRadius"
                Value="16" />
        <Setter Property="Border.BorderThickness"
                Value="2" />
        <Setter Property="Border.Padding"
                Value="2" />
        <Setter Property="Control.Padding"
                Value="2" />
    </Style>
</Window.Resources>
<StackPanel>
    <Border Style="{StaticResource MainBorderStyle}">
        <TextBlock Text="border"/>
    </Border>

    <GroupBox Style="{StaticResource MainBorderStyle}">
        <TextBlock Text="groupBox"/>
    </GroupBox>
</StackPanel>

GroupBox doesn't have CornerRadius, but other properties are applied

Sign up to request clarification or add additional context in comments.

2 Comments

Note however that with a shared Style you are relying on undocumented implementation details of the framework, e.g. that Control.BorderBrush and Border.BorderBrush share the same dependency property registration. Anyway, the implementation will quite certainly never change.
Hi, Thank you very much for your help. It worked as I hoped. Best regards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.