我想将媒体元素的位置绑定到它的模型视图。我知道该属性不是依赖属性。所以试试这种方式,我在网上找到的代码
<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);
}
}
[错误] 无法在“MediaElement”类型的“SetPostion”属性上设置“绑定”。 '绑定'只能在DependencyObject的DependencyProperty上设置。
我做错了什么?
答案 0 :(得分:2)
您遇到的问题是由于您AttachedProperty
的访问者名称错误。
而不是GetPostion
和SetPostion
它们应该是GetPosition
和SetPosition
,而且在使用AttachedProperty时它们应该是local:MediaElementHelper.Position
(不是Postion)。
此外,您还需要按照其他答案的建议更新Type
和default value
。但是,您无需从DependancyObject
派生您的课程,而是可以创建课程static
。
答案 1 :(得分: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);
}
答案 2 :(得分: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);
}
}