简体   繁体   English

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

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

Some native WPF controls have a property category "Text" under which they are listed in the properties inspector, when "Arrange by: Category" is active. 当“排列方式:类别”处于活动状态时,某些本机WPF控件的属性类别为“文本”,它们在属性检查器中列出。 But when I try to set this category for a property of my WPF custom control using 但是当我尝试使用WPF自定义控件的属性设置此类别时

[Category("Text")]

it does not work. 这是行不通的。 The property does not appear in any category. 该属性未出现在任何类别中。 (Tested with VS 2015.) (在VS 2015中进行了测试。)

This is in accordance with the fact that System.ComponentModel.CategoryAttribute does not include a Text category. 这符合System.ComponentModel.CategoryAttribute不包含Text类别的事实。

But how is it then possible to associate a property with the Text category? 但是,如何将属性与“文本”类别相关联?

Edit: For clarification, here is the relevant part of the implementation of the property in the original code: 编辑:为澄清起见,这是原始代码中属性实现的相关部分:

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); }
}

First make sure you are using a Dependency Property. 首先,请确保您使用的是依赖项属性。 If not try typing in dependencyproperty and hit tab (or enter). 如果不是,请尝试键入dependencyproperty,然后单击选项卡(或输入)。 Then define its type and name. 然后定义其类型和名称。

Then you may find these lines of code and add your attribute like this: 然后,您可能会找到以下代码行并添加您的属性,如下所示:

[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