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?
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
.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With