Wpf common border style for all controls

10.5k views Asked by At

I work in wpf
I use the following controls: Button
TextBox
TextBlock
Label
TreeView
ListView
ComboBox
Is there a way to set a default border (In App.xaml) for all of them ?
Thanks, Ilan

3

There are 3 answers

0
sTrenat On

I don't know other way than just setting for each type something like:

<Style TargetType="{x:Type Button}">
    <Setter Property="BorderThickness" Value="3"/>
    <Setter Property="BorderBrush" Value="Green"/>
</Style>

or if you already have some default style for e.g. button

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="BorderThickness" Value="3"/>
    <Setter Property="BorderBrush" Value="Green"/>
</Style>

This will set default BorderThickness and BorderBrush for all buttons in your apps. You can also define OnMouseOver etc. behaviour here.

For TextBlock, you will have to define template too if you want to have some border, because TextBlock is primitive control, without Border. If you want border you can use Label, which is just Textblock with border outside.

0
user3315504 On

Thanks to all
The solution for defining Style for the border works
The only exception is the TextBox because the TextBox does not have a border in it
The solution I found for the TextBox is to define the TextBox style as follows

<Style TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <!-- Modify default template, to change triggers -->
                    <ControlTemplate TargetType="{x:Type TextBoxBase}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Ilan

0
Spawn On

For everything, that use standart Border, you can create this:

<Style TargetType="{x:Type Border}">
    <Setter Property="BorderThickness" Value="10"/>
</Style>

If in any control Border created in other way, you will need to override it manually.