Taufiq Abdur Rahman
Taufiq Abdur Rahman

Reputation: 1388

Binding a property with is not a dependency property

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?

Upvotes: 5

Views: 1848

Answers (3)

Taufiq Abdur Rahman
Taufiq Abdur Rahman

Reputation: 1388

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

Changes Made

public static class MediaElementHelper 
{
    private static readonly TimeSpan DefaultValue = new TimeSpan(0);

    public static readonly DependencyProperty PositionProperty =
        DependencyProperty.RegisterAttached("Position",
        typeof(TimeSpan), typeof(MediaElementHelper),
        new FrameworkPropertyMetadata(DefaultValue, PositionPropertyChanged));

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

        if (richEditControl != null)
        {
            richEditControl.Position = (TimeSpan)e.NewValue;
        }
    }
    public static void SetPosition(UIElement element, TimeSpan value)
    {
        element.SetValue(PositionProperty, value);
    }

    public static TimeSpan GetPosition(UIElement element)
    {
        return (TimeSpan)element.GetValue(PositionProperty);
    }
}

Upvotes: 0

Nitin Purohit
Nitin Purohit

Reputation: 18580

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.

Upvotes: 5

Aron
Aron

Reputation: 15772

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);
}

Upvotes: 1

Related Questions