【问题标题】:How to show values for custom property during design-time?如何在设计时显示自定义属性的值?
【发布时间】:2015-03-31 18:11:15
【问题描述】:

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

并且这个属性可以取值为 Mercury、Venus、Earth 和 Mars。

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

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

【问题讨论】:

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


【解决方案1】:

您需要创建一个 Enum 类型的依赖属性。参考下面的代码。

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

}

【讨论】:

  • 这很有帮助。非常感谢你。您能否也给我一些好的参考资料,我可以在哪里学习更多类似的东西?
【解决方案2】:

这是你需要的:

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

    public string earth{ get; set; }

    public string Jup{ get; set; }

    public string pluto{ get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多