Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Binding for Custom Attached Property

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

like image 965
Jatin Avatar asked Mar 27 '15 16:03

Jatin


1 Answers

Using an attached property as binding source requires to use parentheses in the property path. You'll have to use a regular binding instead of a TemplateBinding:

<Image Source="{Binding Path=(gs:ButtonExtensions.ImagePressedSource),
                        RelativeSource={RelativeSource TemplatedParent}}"/>

Note also that your ButtonExtensions class does not need to be derived from DependencyObject when it only declares attached properties.

It is also recommended to declare DependencyProperty fields read-only:

public static readonly DependencyProperty ImagePressedSourceProperty = ...
like image 98
Clemens Avatar answered Dec 19 '22 10:12

Clemens