我有一个类层次结构,如下所示,绑定到VisibleRange属性是在设计器中抛出.
鉴于此处的类层次结构:
// Base class
public abstract class AxisBase : ContentControl, IAxis
{
public static readonly DependencyProperty VisibleRangeProperty = DependencyProperty.Register(
"VisibleRange", typeof(IRange), typeof(AxisBase),
new PropertyMetadata(default(IRange), OnVisibleRangeChanged));
public IRange VisibleRange
{
get { return (IRange)GetValue(VisibleRangeProperty); }
set { SetValue(VisibleRangeProperty, value); }
}
}
// Derived class
public class DateTimeAxis : AxisBase
{
public new IRange<DateTime> VisibleRange
{
get { return (IRange<DateTime>)GetValue(VisibleRangeProperty); }
set { SetValue(VisibleRangeProperty, value); }
}
}
// And interface definitions
public interface IRange<T> : IRange
{
}
Run Code Online (Sandbox Code Playgroud)
和设计师(XAML)在这里:
<local:DateTimeAxis Style="{StaticResource XAxisStyle}"
VisibleRange="{Binding ElementName=priceChart,
Path=XAxis.VisibleRange, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
无法在"DateTimeAxis"类型的"VisibleRange"属性上设置"绑定".'绑定'只能在DependencyObject的DependencyProperty上设置.
派生类DateTimeAxis暴露VisibleRange属性,该属性被new关键字覆盖.我无法在基AxisBase类中添加泛型类型参数,我还需要在两个类中访问该属性.所以,我想知道给定这些约束,如果有人有任何建议如何更好地避免设计师异常?
Hen*_*man 10
"依赖属性"是您注册的内容:
public static readonly DependencyProperty VisibleRangeProperty =
DependencyProperty.Register("VisibleRange", typeof(IRange), typeof(AxisBase), ...);
Run Code Online (Sandbox Code Playgroud)
当你看到那个陈述时,你可以看到它正在注册 typeof(IRange)
派生类DateTimeAxis公开VisibleRange属性,该属性被new关键字覆盖.
是的,但它暴露了"正常"属性,而不是依赖属性.
另一个因素是属性具有不同的类型.