I have got an application with tons and tons of buttons which I want to make a bit more fancy with hover/click effects. I was hoping one could use a styling template for this so I wouldn't have to write the triggers for each and every button, but I am kind of stuck in this phase.
I think you get the general idea of what I am trying to accomplish here with this code snippet:
<Button BorderBrush="#00000000" Foreground="#00000000" Height="20" HorizontalContentAlignment="Right" IsEnabled="True" Name="btnMinimizeWindow" Width="21" DockPanel.Dock="Right" Margin="0,0,4,2" BorderThickness="0" Focusable="False" Style="{StaticResource ModernButton}">
<Button.Background>
<ImageBrush ImageSource="/MyApp;component/Images/btnMinimize.png" />
</Button.Background>
<Button.Resources>
<DataTemplate x:Key="Default" >
<Image Source="/MyApp;component/Images/btnMinimize.png" />
</DataTemplate>
<DataTemplate x:Key="Hover">
<Image Source="/MyApp;component/Images/btnMinimizeHover.png" />
</DataTemplate>
<DataTemplate x:Key="Active">
<Image Source="/MyApp;component/Images/btnMinimizeActive.png" />
</DataTemplate>
</Button.Resources>
</Button>
and the template file:
<Style x:Key="ModernButton" TargetType="{x:Type Button}">
<Setter Property="Padding" Value="1"/>
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="border" Background="{TemplateBinding Background}">
<ContentPresenter Name="Content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
</Trigger>
</Trigger>
<Trigger Property="IsPressed" Value="True">
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
How do I go about this problem? Is it even possible to do it this way or do I have to clutter my code with millions of triggers?
See Question&Answers more detail:os