为什么我的自定义控件的 Text 属性不会显示在“属性"窗口中?

Why won#39;t my Custom Control#39;s Text property show up in the Properties window?(为什么我的自定义控件的 Text 属性不会显示在“属性窗口中?)
本文介绍了为什么我的自定义控件的 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 属性不会显示在“属性"窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)