C
C#4w ago
malkav

✅ Trying to edit button onMouseOver styles, but to no avail

I'm trying to get my MouseOver effects altered in my styles, but so far I've not been able to edit the mouseover styles. It keeps reverting back to the default blue style when IsMouseOver = true My current style (attempt?)
<Style TargetType="{x:Type Button}" x:Key="BtnOutlinePrimary">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="{DynamicResource CtPrimary}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Button Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{DynamicResource BorderThickness}"
FontFamily="{DynamicResource Roboto-Regular}"
Foreground="Black" >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>

<!-- On Hover -->
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource CtPrimary}" />
<Setter Property="BorderBrush" Value="{DynamicResource CtDark}" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type Button}" x:Key="BtnOutlinePrimary">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="{DynamicResource CtPrimary}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Button Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{DynamicResource BorderThickness}"
FontFamily="{DynamicResource Roboto-Regular}"
Foreground="Black" >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>

<!-- On Hover -->
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource CtPrimary}" />
<Setter Property="BorderBrush" Value="{DynamicResource CtDark}" />
</Trigger>
</Style.Triggers>
</Style>
20 Replies
Buddy
Buddy4w ago
You cannot set the property directly when using a style, it must be set in the style.
malkav
malkav4w ago
I'm sorry, what? 😅 Isn't it in the style?
Buddy
Buddy4w ago
Oh, my apologies.
malkav
malkav4w ago
I just got a little confused 😅 I've also tried doing:
<ControlTemplate>
<!-- ... -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<!-- ... -->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate>
<!-- ... -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<!-- ... -->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
To no avail
leowest
leowest4w ago
This is just an example
<Style TargetType="Button" x:Key="SampleButtonStyle">
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderThickness="0"
Background="{TemplateBinding Background}"
CornerRadius="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" Value="Purple" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="Button" x:Key="SampleButtonStyle">
<Setter Property="Background" Value="Black"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderThickness="0"
Background="{TemplateBinding Background}"
CornerRadius="0">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" Value="Purple" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
to manipulate the ispressed/ismouseover
malkav
malkav4w ago
How come this one uses border instead of button in the template?
leowest
leowest4w ago
I dont know where u got yours from but this is the actual button template if u have a button inside a button it wouldn't make much sense
malkav
malkav4w ago
I've been pretty much doing the F around and find out thing 😅 I saw the border one as well, but wanted to add to the styles but fair enough, lemme try to fiddle around again
leowest
leowest4w ago
well what u normally do is u right click the button in the designer and go to mmmm I forgot the name sec
leowest
leowest4w ago
No description
leowest
leowest4w ago
Edit a Copy
malkav
malkav4w ago
I am not using a designer 😅 I am using Rider, and afaik Rider does not have a designer
leowest
leowest4w ago
and then it gives u the full template of the button
leowest
leowest4w ago
malkav
malkav4w ago
Yay! This worked! Thanks 😅 I just had to use Border instead of Button :catfacepalm: which I guess makes sense now
leowest
leowest4w ago
and this is what the full template looks like so as u can see there the default is
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
u can also see this on the docs
leowest
leowest4w ago
Button Styles and Templates - WPF .NET Framework
Learn about styles and templates for the Windows Presentation Foundation Button control. Modify the ControlTemplate to give the control a unique appearance.
malkav
malkav4w ago
Fair enough. Thanks for sharing that docs
leowest
leowest4w ago
no worries 😉 $close
MODiX
MODiX4w ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server
More Posts