【问题标题】:WPF custom control: How to assign the category "Text" to a property?WPF 自定义控件:如何将类别“文本”分配给属性?
【发布时间】:2019-05-20 02:30:49
【问题描述】:

当“排列方式:类别”处于活动状态时,某些本机 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); }
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    首先确保您使用的是依赖属性。如果没有尝试输入 dependencyproperty 并点击标签(或输入)。然后定义它的类型和名称。

    然后你可能会找到这些代码行并像这样添加你的属性:

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

    【讨论】:

    • 您的建议无效。当然,我使用的是DependencyProperty。但即使我不这样做,也没有什么区别。使用类别“行为”而不是“文本”,该属性按预期显示,与DependencyProperty 关联与否无关。添加描述也没有区别。
    猜你喜欢
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多