0

I have Attached Property and ControlTemplate using the attached property. Attached Propery:

using System.Windows;

namespace NoteProjectV2.classes.frmtopicclasses
{
    class togglebuttonimage : DependencyObject
    {
        public static readonly DependencyProperty togglebuttonimagesource = DependencyProperty.RegisterAttached("ImageSource", typeof(string), typeof(togglebuttonimage), new PropertyMetadata(default(string)));


        public static void Settogglebuttonimagesource(UIElement element, string value)
        {
            element.SetValue(togglebuttonimagesource, value);
        }

        public static string Gettogglebuttonimagesource(UIElement element)
        {
            return (string)element.GetValue(togglebuttonimagesource);
        }


    }
}

This is my control template (I used this in togglebutton)

<Application x:Class="NoteProjectV2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:NoteProjectV2"
             xmlns:m="clr-namespace:NoteProjectV2.classes.frmtopicclasses"
             StartupUri="frmTopic.xaml">
    <Application.Resources>

        <Style x:Key="togglebutton_topic_menu_normal" TargetType="ToggleButton">
            <Setter Property="Width" Value="40" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Border Name="border">
                            <Border.Style>
                                <Style>
                                    <Setter Property="Border.Background" Value="Black"/>

                                </Style>
                            </Border.Style>
                            <Image Width="22" Height="22" Name="image" >
                                <Image.Style>
                                    <Style>
   Only This is not working===============>  <Setter Property="Image.Source" Value="{Binding Path=(m:togglebuttonimage.togglebuttonimagesource),RelativeSource={RelativeSource TemplatedParent}}" />
                                    </Style>
                                </Image.Style>
                            </Image>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>

I used attached propery in code:I also added namespace above

xmlns:m="clr-namespace:NoteProjectV2.classes.frmtopicclasses"
<ToggleButton Style="{StaticResource togglebutton_topic_menu_normal}" m:togglebuttonimage.togglebuttonimagesource="accept.png" />

But this is not working Where is my problem?

eoweww
  • 117
  • 2
  • 11
  • 1
    You do not follow the naming convention of dependency properties. Please have a look [here in SO](http://stackoverflow.com/questions/7139641/how-to-use-attached-property-within-a-style) – gomi42 Oct 29 '16 at 18:11
  • And you certainly agree that `togglebuttonimagesource` is a horribly name of a property, not to mention method names lile `Settogglebuttonimagesource`. Use [Pascal Casing](https://msdn.microsoft.com/en-us/library/ms229043(v=vs.110).aspx) instead. – Clemens Oct 30 '16 at 19:23

1 Answers1

1

The first argument to the Register and RegisterAttached method of class DependencyProperty is the name of the property.

While you are using the name "ImageSource", it should actually be "ToggleButtonImageSource" (which already uses proper casing). Note also that as long as you only declare attached properties, the owning class does not need to be derived from DependencyObject.

public class ToggleButtonImage
{
    public static readonly DependencyProperty ToggleButtonImageSourceProperty =
        DependencyProperty.RegisterAttached(
            "ToggleButtonImageSource", typeof(string), typeof(ToggleButtonImage));

    public static void SetToggleButtonImageSource(UIElement element, string value)
    {
        element.SetValue(ToggleButtonImageSourceProperty, value);
    }

    public static string GetToggleButtonImageSource(UIElement element)
    {
        return (string)element.GetValue(ToggleButtonImageSourceProperty);
    }
}

Besides that, you should better use ImageSource instead of string as the type of the property.

Clemens
  • 123,504
  • 12
  • 155
  • 268