Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a property with is not a dependency property

Tags:

c#

wpf

I want to bind Position of media element to it model view. I know about that property not being a dependency property. So tried it this way, a code i found on the net

<MediaElement Source="{Binding CurrentClip.Path, Converter={StaticResource converter}, UpdateSourceTrigger=PropertyChanged}" Stretch="Uniform" local:MediaElementHelper.Postion="{Binding CurrentClip.Postion}"

MediaElementHelper

class MediaElementHelper
{
    public static readonly DependencyProperty PostionProperty =
        DependencyProperty.RegisterAttached("Position",
        typeof(bool), typeof(MediaElement),
        new FrameworkPropertyMetadata(false, PostionPropertyChanged));

    private static void PostionPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var richEditControl = obj as MediaElement;

        if (richEditControl != null)
        {
            richEditControl.Position = (TimeSpan)e.NewValue;
        }
    }
    public static void SetPostion(UIElement element, TimeSpan value)
    {
        element.SetValue(PostionProperty, value);
    }
    public static TimeSpan GetPostion(UIElement element)
    {
        return (TimeSpan)element.GetValue(PostionProperty);
    }
}

[Error] A 'Binding' cannot be set on the 'SetPostion' property of type 'MediaElement'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

What am i doing wrong?

like image 801
Taufiq Abdur Rahman Avatar asked Oct 23 '13 05:10

Taufiq Abdur Rahman


2 Answers

The problem you are facing is due to the fact that you have wrong accessors names for your AttachedProperty.

Instead of GetPostion and SetPostion they should be GetPosition and SetPosition and offcourse similarly while using the AttachedProperty it should be local:MediaElementHelper.Position (not Postion).

Also you will need to update your Type and default value as suggested by other answers. But there is no need to derive your class from DependancyObject, rather you can make your class static.

like image 147
Nitin Avatar answered Sep 29 '22 04:09

Nitin


The only thing wrong I can see is your Attached Property is not registered to the right type.

public static readonly DependencyProperty PostionProperty =
    DependencyProperty.RegisterAttached("Position",
    typeof(TimeSpan), typeof(MediaElement),
    new FrameworkPropertyMetadata(TimeSpan.FromSecond(0), ChangeHandler));

Figured I should put up a template for Attached Properties....

public class AttachedPropertyClass 
{
    private static readonly {PropertyType} DefaultValue = ...;

    public static readonly DependencyProperty {PropertyName}Property
       = DependencyProperty.RegisterAttached("{PropertyName}",
    typeof({PropertyType}), typeof({AttachedType}),
    new FrameworkPropertyMetadata(DefaultValue, PostionPropertyChanged));;

    public static void Set{PropertyName}(DependencyObject d, {PropertyType} value)
    {
        d.SetValue({PropertyName}, value);
    }

    public static {PropertyType} Get{PropertyName}(DependencyObject DepObject)
    {
        return ({PropertyType})DepObject.GetValue({PropertyName});
    }

    private static void ChangeHandler(DependencyObject obj, DependencyPropertyChangedEventArgs e);
}
like image 24
Aron Avatar answered Sep 29 '22 04:09

Aron