【发布时间】:2013-10-09 06:41:39
【问题描述】:
如何使依赖对象属性数组可绑定,以便以后绑定为静态资源?
我现在的代码,看来我的DependencyObject绕过了依赖属性系统……
我有以下课程:
public class ValueMarker : DependencyObject
{
public static readonly DependencyProperty BrushProperty = DependencyProperty.Register("Brush", typeof(Brush), typeof(ValueMarker), new FrameworkPropertyMetadata(Brushes.Aqua));
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(ValueMarker), new FrameworkPropertyMetadata(0d));
public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register("Offset", typeof(double), typeof(ValueMarker), new FrameworkPropertyMetadata(0d));
public Brush Brush
{
get { return (Brush)GetValue(BrushProperty); }
set { SetValue(BrushProperty, value); }
}
public double Offset
{
get { return (double)GetValue(OffsetProperty); }
set { SetValue(OffsetProperty, value); }
}
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
}
在 XAML 中,我创建了一个资源数组,其中包含一些绑定,如下所示:
<x:Array Type="my:ValueMarker" x:Key="plainMarks">
<my:ValueMarker Brush="Red" Offset="-5" Value="{Binding Path=...}" />
<my:ValueMarker Brush="Orange" Offset="-5" Value="{Binding Path=...}"/>
<my:ValueMarker Brush="Orange" Offset="-5" Value="{Binding Path=...}"/>
<my:ValueMarker Brush="Red" Offset="-5" Value="{Binding Path=...}" />
</x:Array>
在调试绑定时,我注意到如果我删除 DP 的设置器,XAML 将显示一个错误,指出该属性丢失。我的理解是 XAML 使用 DP 系统来分配值从而启用绑定。在这种情况下,如果 XAML 需要“正常”属性,则无法进行绑定。任何人都可以告诉我如何使它工作?
【问题讨论】:
标签: c# wpf xaml dependency-properties dependencyobject