为什么我的自定义控件的 Text 属性不会显示在“属性"窗口中? [英] Why won't my Custom Control's Text property show up in the Properties window?

查看:29
本文介绍了为什么我的自定义控件的 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 属性不会显示在“属性"窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆