Why won#39;t my Custom Control#39;s Text property show up in the Properties window?(为什么我的自定义控件的 Text 属性不会显示在“属性窗口中?)
问题描述
我有一个继承自 UserControl 的用户控件.它是一个按钮,所以我试图让按钮中的文本,通过像真正的按钮一样使用 Text 属性来更改,而不是像 _Text 一样命名我自己的.我有以下代码,但它不起作用(即它没有出现在属性窗口中).标签的名称是 ContentPresenter
I have a user control that inherits from UserControl. It's a button so I'm trying to make the text in the button, change-able by using the Text property like the real buttons, instead of naming my own like _Text. I have the following code but it doesn't work (ie it doesn't show up in the Property Window). The name of the label is ContentPresenter
public override string Text
{
get
{
return ContentPresenter.Text;
}
set
{
ContentPresenter.Text = value;
}
}
推荐答案
UserControl 努力隐藏 Text 属性.来自元数据:
UserControl goes to significant effort to hide the Text property. From the metadata:
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
public override string Text { get; set; }
您可以通过在代码中覆盖这些属性来使其可见:
You can make it visible by overriding those attributes in your code:
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[EditorBrowsable(EditorBrowsableState.Always)]
[Bindable(true)]
public override string Text
{
get { return ContentPresenter.Text; }
set { ContentPresenter.Text = value; }
}
我不保证这足以让它发挥作用,但它可能是.
I'm not promising that's enough to make it work, but it probably is.
这篇关于为什么我的自定义控件的 Text 属性不会显示在“属性"窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我的自定义控件的 Text 属性不会显示在“属性"窗口中?


基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01