C# 将属性与绑定不是依赖项属性
我想将媒体元素的位置绑定到它的模型视图。我知道那个属性不是依赖属性。所以我试着这样做,我在网上找到了一个代码C# 将属性与绑定不是依赖项属性,c#,wpf,C#,Wpf,我想将媒体元素的位置绑定到它的模型视图。我知道那个属性不是依赖属性。所以我试着这样做,我在网上找到了一个代码 <MediaElement Source="{Binding CurrentClip.Path, Converter={StaticResource converter}, UpdateSourceTrigger=PropertyChanged}" Stretch="Uniform" local:MediaElementHelper.Postion="{Binding Curren
<MediaElement Source="{Binding CurrentClip.Path, Converter={StaticResource converter}, UpdateSourceTrigger=PropertyChanged}" Stretch="Uniform" local:MediaElementHelper.Postion="{Binding CurrentClip.Postion}"
[错误]
无法在“MediaElement”类型的“SetPostion”属性上设置“Binding”。只能对DependencyObject的DependencyProperty设置“绑定”
我做错了什么 我能看到的唯一错误是您的附加属性没有注册到正确的类型
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);
}
您所面临的问题是由于您的
AttachedProperty
的访问者名称错误
与GetPosition
和SetPosition
不同,它们应该是GetPosition
和SetPosition
,当然,在使用附件属性时,它们应该是local:MediaElementHelper.Position
(非Position)
此外,您还需要按照其他答案的建议更新您的
类型
和默认值
。但是没有必要从DependancyObject
派生您的类,相反,您可以使您的类static
我得到一个新的错误默认值类型与属性“Positionwhoops”的类型不匹配……您是对的……在两个地方您得到了错误的类型,返回类型(显式)和默认值(错,暗示仍然是bool)。我已经更新了我的答案。
<MediaElement Source="{Binding CurrentClip.Path, Converter={StaticResource converter}, UpdateSourceTrigger=PropertyChanged}" Stretch="Uniform" local:MediaElementHelper.Position ="{Binding CurrentClip.Postion}">
<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);
}
}