【问题标题】:How do I get a custom property to appear in the property manager?如何让自定义属性出现在属性管理器中?
【发布时间】:2015-08-24 04:34:29
【问题描述】:

我正在使用 Visual Studio 2015 Community 在 C# 中使用 Windows 窗体创建应用程序。我创建了一个具有公共属性和设置的自定义控件(实现一个按钮)。当我将自定义按钮添加到表单时,如何让这些自定义属性/设置显示在 Visual Studio 属性管理器中?

我有私人支持者和公共访问者,就像通常做的那样;

多年前我曾经这样做过,但我忘记了如何让它出现在物业管理器中。默认情况下,VS 2015 和以前的版本,属性管理器是通常设置标准控件属性的区域,位于 IDE 的右下角。

这是我正在使用的属性之一的 sn-p:

public class Spot : Button
{
    // backers for the properties
    private bool isselected = false;

    // //////////////////////////////////////////////////////////////
    // user accessable properties relying on the above backers
    //

    /// <summary>
    /// Get/Set if the spot is selected
    /// </summary>
    public bool isSelected
    {
        get
        {
            return isselected;
        }
        set
        {
            isselected = value;
        }
    }
}

希望这里有人可以帮助提醒我如何操作。我确实在这里进行了几次搜索,并通过谷歌搜索,但似乎没有地方有我要找的东西。通过包括“物业经理”在内的特定搜索,我得到了许多与我正在寻找的结果相去甚远的房地产结果。

【问题讨论】:

    标签: c# winforms visual-studio properties custom-controls


    【解决方案1】:

    您需要使用 DependencyProperty。取自here

    // Dependency Property
    public static DependencyProperty CurrentTimeProperty = 
         DependencyProperty.Register( "CurrentTime", typeof(DateTime),
         typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));
    
    // .NET Property wrapper
    public DateTime CurrentTime
    {
        get { return (DateTime)GetValue(CurrentTimeProperty); }
        set { SetValue(CurrentTimeProperty, value); }
    }
    

    这将为您提供一个新属性“CurrentTime”作为示例。

    你的应该是这样的:

    public static DependencyProperty SelectedProperty = 
         DependencyProperty.Register( "IsSelected", typeof(Boolean),
         typeof(Spot), new FrameworkPropertyMetadata(false));
    
    public Boolean IsSelected
    {
        get { return (Boolean)GetValue(SelectedProperty); }
        set { SetValue(SelectedProperty, value); }
    }
    

    --- 编辑,因为它是用于 WinForms:

    试试看here:

    [EditorBrowsable(EditorBrowsableState.Always)]
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [Bindable(true)]
    public bool isSelected
    {
        get
        {
            return isselected;
        }
        set
        {
            isselected = value;
        }
    }
    

    【讨论】:

    • 感谢您的回复,但我应该澄清一下我没有使用 WPF。如果有意义的话,我正在使用标准的 Windows 窗体。我应该编辑我的问题以反映这一点吗?还是让它更通用,并根据这个问题为各种项目类型提供多个答案?
    • 对不起,请添加您使用WinForms的信息。
    • 漂亮,使用您对 winforms 的编辑就像一个魅力。猜猜你用一块石头杀死了两只鸟。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    相关资源
    最近更新 更多