5

我想将媒体元素的位置绑定到它的模型视图。我知道该属性不是依赖属性。于是就这样试了一下,网上找的一个代码

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

媒体元素助手

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

[错误] 无法在“MediaElement”类型的“SetPostion”属性上设置“绑定”。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。

我究竟做错了什么?

4

3 回答 3

4

您面临的问题是由于您的AttachedProperty.

在使用 AttachedProperty 时,它们应该GetPostionSetPostion它们应该是相似的,而不是它们应该是(GetPosition而不是 Postion)。SetPositionlocal:MediaElementHelper.Position

此外,您还需要按照其他答案的建议更新您的Type和。default value但是没有必要从你的类派生DependancyObject,而是你可以制作你的类static

于 2013-10-23T06:09:27.823 回答
1

我能看到的唯一错误是您的附加属性未注册到正确的类型。

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

想我应该为附加属性建立一个模板......

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);
}
于 2013-10-23T05:50:25.557 回答
0
<MediaElement Source="{Binding CurrentClip.Path, Converter={StaticResource converter}, UpdateSourceTrigger=PropertyChanged}" Stretch="Uniform" local:MediaElementHelper.Position ="{Binding CurrentClip.Postion}">

所做的更改

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);
    }
}
于 2013-10-23T06:34:52.303 回答