2011-09-21 32 views
1

我曾嘗試在DependencyProperty中使用Enum,但始終需要Enum的第一個值。爲什麼Enum在DependencyProperty中返回Enum的第一個值?

例如 我的枚舉:

public enum LayoutType 
{ 
    Horizontal, 
    Vertical 
} 

財產申報:

public static readonly DependencyProperty LayoutTypeProperty = 
     DependencyProperty.RegisterAttached("LayoutType", typeof(LayoutType), typeof(ctrlAllLayouts), new PropertyMetadata(null)); 

我可以在我的XAML,但問題訪問屬性是它總能給值「水平」,如果將其設置是要麼「水平」或「垂直」。你還必須聲明匹配靜態 setter和getter方法爲它解析

+0

+1提供代碼和良好的描述 –

回答

0

隨着Attached Properties(與你RegisterAttached這些都不是普通的依賴屬性)。 Xaml解析器實際上使用這些方法。

例如

public static void SetLayoutType(DependencyObject element, LayoutType value) 
{ 
    element.SetValue(LayoutTypeProperty, value); 
} 
public static LayoutType GetLayoutType(DependencyObject element) 
{ 
    return (LayoutType)element.GetValue(LayoutTypeProperty); 
} 

如果這些方法都失蹤了,因爲你沒有指定在PropertyMetadata的默認值,它總是被設置爲0,這是你的第一個枚舉值。

相關問題