[英]Binding a property with is not a dependency property
我想将媒体元素的位置绑定到它的模型视图。 我知道该属性不是依赖项属性。 所以这样尝试,我在网上找到了一个代码
<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'属性上设置'Binding'。 只能在DependencyObject的DependencyProperty上设置“绑定”。
我究竟做错了什么?
您面临的问题是由于您的AttachedProperty
访问器名称错误。
代替GetPostion
和SetPostion
它们应该分别是GetPosition
和SetPosition
并且在使用AttachedProperty时,它们应该是GetPosition
local:MediaElementHelper.Position
(不是Postion)。
另外,您还需要根据其他答案的建议更新“ Type
和default value
。 但是不需要从DependancyObject
派生您的类,而是可以使您的类static
。
我唯一看到的错误是您的附加财产未注册为正确的类型。
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);
}
<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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.