2011-05-05 68 views
11

Tôi đang cố gắng để có được một DependencyProperty làm việc trong WPF. Tôi đang sử dụng:DependencyProperty Giá trị mặc định

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); } 
    } 

Khi tôi khởi tạo cửa sổ, tôi gặp lỗi: Loại giá trị mặc định không khớp với loại thuộc tính 'DisplayMode'. Tuy nhiên, nếu tôi để lại giá trị mặc định, tôi nhận được một ngoại lệ tham chiếu null khi cửa sổ tải do DisplayModeProperty không được thiết lập.

+1

Tham số thứ hai là tài sản loại, tham số thứ ba là loại điều khiển, chú ý, hoán đổi chúng trong ví dụ của bạn. – vorrtex

+0

Đó là một sai lầm ngu ngốc. Cảm ơn. – Echilon

+0

@vorrtex: Vui lòng đăng câu trả lời đó ... –

Trả lời

13

Đăng nhận xét dưới dạng câu trả lời.

Theo MSDN DependencyProperty.Register Method cú pháp trông vậy:

public static DependencyProperty Register(
    string name, 
    Type propertyType, 
    Type ownerType, 
    PropertyMetadata typeMetadata 
) 

Trong trường hợp của bạn ownerType là TescoFoodSummary và PropertyType là Orientation, vì vậy các thông số có các vị trí sau:

DependencyProperty.Register("DisplayMode", typeof (Orientation), typeof (TescoFoodSummary), new UIPropertyMetadata(Orientation.Vertical)); 
+0

Cảm ơn, một lỗi dễ mắc phải. – Echilon

Các vấn đề liên quan