Click here to Skip to main content
15,883,766 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm making a style for my buttons and when I added this piece of code

XAML
<Setter Property="Template">
	<Setter.Value>
		<ControlTemplate TargetType="{x:Type Button}">
			<ControlTemplate.Triggers>
				<Trigger Property="IsMouseOver" Value="True">
					<Setter Property="Background" Value="#733847"></Setter>
				</Trigger>
				<Trigger Property="IsEnabled" Value="False">
					<Setter Property="Background" Value="#3b1922"></Setter>
				</Trigger>
				<Trigger Property="IsPressed" Value="True">
					<Setter Property="Background" Value="#592533"></Setter>
				</Trigger>
			</ControlTemplate.Triggers>
		</ControlTemplate>
	</Setter.Value>
</Setter>

all my buttons just disappeared for some reason

How should I fixed this? all help would be appreciated

What I have tried:

I've tried deleted the
Property="Template"
but that just gave me an error and my program won't run but the buttons reappear on the preview thing on the xaml editor
Posted
Updated 25-Mar-22 0:19am
v2

1 solution

By applying this style you are setting the template for all buttons.
But there it is not defined how the content must be displayed.
Therefore the button seems to have disappeared.
XAML
<Window.Resources>
	<Style TargetType="Button">
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="{x:Type Button}">
					<ControlTemplate.Triggers>
						<Trigger Property="IsMouseOver" Value="True">
							<Setter Property="Background" Value="#733847"></Setter>
						</Trigger>
						<Trigger Property="IsEnabled" Value="False">
							<Setter Property="Background" Value="#3b1922"></Setter>
						</Trigger>
						<Trigger Property="IsPressed" Value="True">
							<Setter Property="Background" Value="#592533"></Setter>
						</Trigger>
					</ControlTemplate.Triggers>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</Window.Resources>


It is assumed that you just want to change the background when e.g. the mouse is over the button.
Here is how to accomplish that.
XAML
<Window.Resources>
	<Style TargetType="Button">
		<Style.Triggers>
			<Trigger Property="IsMouseOver" Value="True">
				<Setter Property="Background" Value="#733847"></Setter>
			</Trigger>
			<Trigger Property="IsEnabled" Value="False">
				<Setter Property="Background" Value="#3b1922"></Setter>
			</Trigger>
			<Trigger Property="IsPressed" Value="True">
				<Setter Property="Background" Value="#592533"></Setter>
			</Trigger>
		</Style.Triggers>
	</Style>
</Window.Resources>

You should follow this tutorial in order to understand how WPF triggers work.
WPF - Triggers[^]
 
Share this answer
 
Comments
Thegreatfish 25-Mar-22 6:21am    
that doesn't seem to really work for me since the code I showed was just a snippet of this

<window.resources>


<Setter Property="Background" Value="#47242d"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="FontFamily" Value="Fonts\Halyard Display Regular"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Width" Value="200"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#733847"></Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="#3b1922"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="#592533"></Setter>
</Trigger>
</Controltemplate.Triggers>
</Controltemplate>
</Setter.Value>
</Setter>

TheRealSteveJudge 25-Mar-22 6:32am    
Even this new snippet is not complete and will not compile at all.
BTW <window.resources> must be <window.resources>
TheRealSteveJudge 25-Mar-22 6:34am    
BTW: ahfafjafjajgsgjsjggjjsgsk - Professional Profile
@fishfishfishfishWatchreport looks like SPAM
Why don't you use a decent name?
Thegreatfish 25-Mar-22 16:29pm    
I just used sign in with google and didn't change that 😅

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900