2012-04-05 39 views
6

我有一个类层次结构如下,并绑定到VisibleRange属性抛出设计器。绑定只能在DependencyObject的DependencyProperty上设置 - 当属性被新的覆盖时

鉴于类层次结构在这里:

// 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 
{ 
} 

和名牌(XAML)位置:

<local:DateTimeAxis Style="{StaticResource XAxisStyle}"            
     VisibleRange="{Binding ElementName=priceChart, 
         Path=XAxis.VisibleRange, Mode=TwoWay}"/> 

我得到这个异常:

A '绑定' 不能设置类型'DateTimeAxis'的'VisibleRange'属性。 '绑定'只能在DependencyObject的DependencyProperty上设置。

派生类DateTimeAxis暴露了VisibleRange属性,该属性被new关键字覆盖。我无法向基类AxisBase类添加泛型类型参数,并且我还需要访问这两个类中的属性。所以,我想知道这些限制,如果任何人有任何建议,如何更好地做到这一点,以避免设计师的例外?

+0

Andrew Burnett-Thom:你有没有尝试在我的答案中编码?它有用吗? – Tigran 2012-04-05 21:58:54

回答

10

的“依赖项属性”是你注册的事情:

public static readonly DependencyProperty VisibleRangeProperty = 
    DependencyProperty.Register("VisibleRange", typeof(IRange), typeof(AxisBase), ...); 

而且当你看这句话,你可以看到它与typeof(IRange)

派生类的DateTimeAxis是注册暴露由new关键字覆盖的VisibleRange属性。

是的,但是它暴露了一个'正常'属性,而不是一个依赖属性。
另一个因素是属性具有不同的类型。

+0

它暴露了基地的DependencyProperty - 我想这是它出错的地方吧?我可能可以使用新的覆盖,并在整个代码中放置代码 – 2012-04-05 21:41:33

+2

是的,DP是怪兽。我会绕过这个。 – 2012-04-05 21:45:00

+0

完成,并工作:)感谢您的提示! – 2012-04-05 21:47:56

0

尝试XAxis的代码初始化写,像

AxisBase XAxis = new DateTimeAxis()

应该工作。

+0

我很好奇,如果它... – 2012-04-05 21:47:03

+0

@HenkHolterman:其实我也是,将写入OP .. :) – Tigran 2012-04-05 21:58:06

+0

不知道你是什么意思 - DateTimeAxis是在Xaml中构建的,而不是C#代码 – 2012-04-05 22:01:21

相关问题