我想要在WPF中使用DependencyProperty。我正在使用:DependencyProperty默認值
public static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register("DisplayMode", typeof (TescoFoodSummary), typeof (Orientation), new UIPropertyMetadata(Orientation.Vertical));
/// <summary>
/// Gets or sets the orientation.
/// </summary>
/// <value>The orientation.</value>
public Orientation DisplayMode {
get { return (Orientation)base.GetValue(DisplayModeProperty); }
set { base.SetValue(DisplayModeProperty, value); }
}
當我初始化窗口時,出現錯誤:默認值類型與屬性'DisplayMode'的類型不匹配。 Howevere,如果我保留默認值,當由於DisplayModeProperty沒有被設置而導致窗口加載時,我得到一個空引用異常。
第二個參數是屬性類型,第三個參數是控件的類型,請注意,在你的例子中交換它們。 – vorrtex 2011-05-05 14:06:39
那是一個愚蠢的錯誤。謝謝。 – Echilon 2011-05-05 19:57:16
@vorrtex:請將其發佈爲答案... – 2012-08-24 21:09:04