如何在设计时显示自定义属性的值?

时间:2015-03-31 18:11:16

标签: c# wpf properties user-controls design-time

我是WPF C#的新手,我正在尝试使用自定义属性创建用户控件。我有一个名为Planet的房产。

此属性可以取值为水星,金星,地球和火星。

在设计期间,当我在我的应用程序中包含此控件并输入属性名称时,我希望Visual Studio提示这4个值。就像我们使用Alignment属性进行某些控制时一样,它显示了Left,Right,Center ..

任何人都可以建议我如何做到这一点吗?

2 个答案:

答案 0 :(得分:0)

这就是你需要的:

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

    public string earth{ get; set; }

    public string Jup{ get; set; }

    public string pluto{ get; set; }
}

答案 1 :(得分:0)

您需要创建Enum类型的Dependency属性。请参阅下面的代码。

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

}
相关问题