2015-03-31 59 views
0

我是新來WPF C#,我試圖用自定義屬性創建一個用戶控件。我有一個名爲星球的財產。如何在設計時顯示自定義屬性的值?

而這個屬性可以把水銀,金星,地球和火星的價值。

在設計期間,當我在應用程序中包含此控件並鍵入屬性名稱時,我希望Visual Studio提示這4個值。就像我們在某些控件上使用Alignment屬性時一樣,它顯示左,右,中心。

任何人都可以告訴我如何做到這一點?

+0

我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2015-03-31 18:14:00

+0

您需要一個枚舉 – 2015-03-31 18:17:05

回答

0

您需要創建類型枚舉的Depedency財產。請參閱以下代碼。

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
    } 


    public Planets Planet 
    { 
     get { return (Planets)GetValue(PlanetProperty); } 
     set { SetValue(PlanetProperty, value); } 
    } 


    public static readonly DependencyProperty PlanetProperty = 
     DependencyProperty.Register("Planet", typeof(Planets), typeof(UserControl1), new UIPropertyMetadata(Planets.Mercury)); 


} 

public enum Planets 
{ 
    Mercury, 
    Venus, 
    Earth, 
    Mars 

} 
+0

這非常有幫助。非常感謝你。你能否給我提供一些很好的參考資料,在這裏我可以學到更多這樣的東西? – amoid 2015-03-31 19:16:30

0

這是你需要的東西:

public class Planet 
{ 
    public string mars{ get; set; } 

    public string earth{ get; set; } 

    public string Jup{ get; set; } 

    public string pluto{ get; set; } 
} 
相關問題