c# - UserControl 在 Visual Studio 设计器中显示属性,但属性未更改

标签 c# winforms visual-studio-2012 user-controls

所以今天,我决定使用 Visual Studio 2012 在 C# 中创建一个自定义 TextButton 控件。我非常喜欢我所做的设计,尽管它可能很简单。然而,当我决定使用 Visual Studio Designer 将完成的组件移动到一个窗体中时,我遇到了麻烦。虽然 Text 属性显示在属性列表中,但该属性实际上并未在 MainWindow.Designer.cs 文件中更改。下面是定义属性的代码:

[Category("Appearance")]
[Description("The text displayed by the control.")] 
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public override string Text
{
    get
    {
        return Button.Text;
    } 
    set
    {
        Button.Text = value;
    }
}

下面是我的 MainWindow.Designer.cs 文件在 InitializeComponent 方法中创建的代码:

this.Open = new UI.Controls.TextButton();
this.Open.Location = new System.Drawing.Point(9, 3);
this.Open.Name = "Open";
this.Open.Size = new System.Drawing.Size(112, 28);
this.Open.TabIndex = 4;
this.Open.Click += new System.EventHandler(this.OpenButton_Click);

最佳答案

部分问题是您覆盖了 UserControl 的 Text 属性。如果您将属性命名为 Text 之外的另一个名称,例如 myText,它将起作用。为了解决您在使用 Text 时遇到的问题,请尝试添加 DesignerSerializationVisibility归属于你的属性(property),那应该照顾它。

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

关于c# - UserControl 在 Visual Studio 设计器中显示属性,但属性未更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19171857/

相关文章:

c# - 如何从第二个重定向页面更新第一个页面?

c# - 使用 C# 进行输入验证和异常

asp.net-mvc-3 - 已发布的 MVC3 应用程序结果为 : "Could not load file or assembly ' System. Web.Mvc'”

c# - Windows 条形码阅读器服务

c# - Shutdown.exe 没有立即关闭系统,为什么?

c# - 在 C#.net 中将 List<DataRow> 转换为 List<UserEntity>

c# - 如何禁止 TreeView.ExpandAll 方法自动滚动到树的末尾?

vb.net - 编译适用于 ARM 和 Windows RT 的 VB.NET WinForms 应用程序

c++ - 无法更改 VC++ CMFCOutlookBar 的 Tab 标签文本

c# - 对于 C#,使用 WSSE 纯文本身份验证的 WCF SOAP 使用者?