繁体   English   中英

WPF自定义控件:如何将类别“文本”分配给属性?

[英]WPF custom control: How to assign the category “Text” to a property?

当“排列方式:类别”处于活动状态时,某些本机WPF控件的属性类别为“文本”,它们在属性检查器中列出。 但是当我尝试使用WPF自定义控件的属性设置此类别时

[Category("Text")]

这是行不通的。 该属性未出现在任何类别中。 (在VS 2015中进行了测试。)

这符合System.ComponentModel.CategoryAttribute不包含Text类别的事实。

但是,如何将属性与“文本”类别相关联?

编辑:为澄清起见,这是原始代码中属性实现的相关部分:

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

...

public static readonly DependencyProperty IsReadOnlyProperty;

...

[Browsable(true)]
[Category("Text")]
[Description("Gets or sets a value that indicates whether the text editing control is read-only to a user interacting with the control.")]
public bool IsReadOnly
{
  get { return (bool)GetValue(IsReadOnlyProperty); }
  set { SetValue(IsReadOnlyProperty, value); }
}

首先,请确保您使用的是依赖项属性。 如果不是,请尝试键入dependencyproperty,然后单击选项卡(或输入)。 然后定义其类型和名称。

然后,您可能会找到以下代码行并添加您的属性,如下所示:

[Description("Your Description"), Category("Text")]
public string PropName {
     get { return (string)GetValue(PropNameProperty); }
     set { SetValue(PropNameProperty, value); 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM