Francefire Francefire - 1 month ago 8
C# Question

How to create a new button style?

Hello so I use a lot of button and I don't like the "animation" when my mouse is over the button so I found this :

<Button.Style>
<Style
TargetType="{x:Type Button}">
<Setter
Property="OverridesDefaultStyle" Value="True"/>
<Setter
Property="Margin" Value="5"/>
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type Button}">
<Border
x:Name="border"
BorderBrush="{x:Null}"
BorderThickness="3"
Background="{TemplateBinding Background}"
CornerRadius="0"
Padding="0">
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger
Property="IsMouseOver"
Value="True">
<Setter
Property="BorderBrush"
TargetName="border"
Value="#252525"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>


It works great, so I was wondering how to create a new Style named "NoMouseOver" that I can just choose here : http://image.noelshack.com/fichiers/2016/42/1477013610-style.png

Answer

Although you could have find the answer to your question on a quick search (here on stackoverflow or google), here is a simple answer to your question:

Create the style in the Resources-section of your Window/UserControl:

<Windows.Resources>
    <Style x:Key="NoMouseOverStyle" TargetType="Button">
        ...
    </Style>
</Window.Resources>

Afterwards you can select this style in the properties, or directly use it in your XAML.

<Button Style={StaticResource NoMouseOverStyle} />

Keep in mind that if you want to use this style in different windows or user controls, you may want to use ResourceDictionarys.

EDIT As Steffen said, i removed the DynamicResource and replaced it with StaticResource.