I am trying to use an Image in Button Control which animates on Hover and Pressed state by showing different images. Accordingly, I have defined 3 attached properties for the Button Control as given below.
public class ButtonExtensions : DependencyObject {
public static DependencyProperty ImageSourceProperty = ...
public static DependencyProperty ImageHoverSourceProperty = ...
public static DependencyProperty ImagePressedSourceProperty =
DependencyProperty.RegisterAttached("ImagePressedSource", typeof(string), typeof(ButtonExtensions));
public static string GetImagePressedSource(Button target) { return (string)target.GetValue(ImagePressedSourceProperty); }
public static void SetImagePressedSource(Button target, string value) { target.SetValue(ImagePressedSourceProperty, value); }
I set these properties in Button's Style property setters as given below
<Style x:Key="AddButtonStyle" TargetType="{x:Type Button}" >
<Setter Property="gs:ButtonExtensions.ImageSource" Value="/HotelReservation.ControlLibrary;component/Images/add-record-icon.png"/>
<Setter Property="gs:ButtonExtensions.ImageHoverSource" Value="/HotelReservation.ControlLibrary;component/Images/add-record-hover-icon.png"/>
<Setter Property="gs:ButtonExtensions.ImagePressedSource" Value="/HotelReservation.ControlLibrary;component/Images/add-record-pressed-icon.png"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Height="32" Width="32">
<!-- How to use TemplateBinding Here. This does not work -->
<Image Name="Normal" Source="{TemplateBinding Property=gs:ButtonExtensions.ImageSource}" />
/>
<!-- This Works -->
<Image Name="Hover" Source="/HotelReservation.ControlLibrary;component/Images/add-record-hover-icon.png" Opacity="0"/>
<Image Name="Pressed" Source="/HotelReservation.ControlLibrary;component/Images/add-record-pressed-icon.png" Opacity="0" />
</Grid>
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
As you can see, I am trying to access the custom attached properties from within the Control Template for the Button. I can get it working by hard coding the Source attribute of the Image control, but I wan't to use TemplateBinding instead