简体   繁体   English

VisualStudio的PropertyWindow中的依赖项属性

[英]Dependency Property in the PropertyWindow of VisualStudio

First I will give you my code and after that I will ask my question. 首先,我将为您提供代码,然后再问我一个问题。

namespace LinearGradientBrushBinding
{
    public partial class LinearGradBrush : UserControl
    {
        public LinearGradBrush()
        {
            InitializeComponent();
        }

        class LinearGradBrushProp : DependencyObject
        {
            public static DependencyProperty _background;

            static void BackgroundBrush()
            {
                _background = DependencyProperty.Register(
                    "_background", typeof(Brush), typeof(LinearGradBrushProp));
            }

            [Description("CuloareBG"), Category("Z")]
            public Brush Background
            {
                get { return (Brush)GetValue(_background); }
                set { SetValue(_background, value); }
            }
        }
    }
}

As you can see I have an UserControl which has in it a class. 如您所见,我有一个UserControl,其中包含一个类。 My question is why I don't see in the Property window (right side of UserControl.Xaml) of my control the category Z with a brush in it. 我的问题是为什么我在控件的“属性”窗口(UserControl.Xaml的右侧)中看不到类别Z,其中包含画笔。

why I don't see in the Property window (right side of UserControl.Xaml) of my control the category Z with a brush in it. 为什么在控件的“属性”窗口(UserControl.Xaml的右侧)中看不到类别Z,其中包含画笔。

Quite simply because your LinearGradBrush doesn't contain a property annotated with the category Z . 很简单,因为您的LinearGradBrush不包含带有类别Z注释的属性。

Your LinearGradBrush contains a (private) inner class that does have such a property, but there's no way the property editor can know which instance of this inner class to assign a property value to. 您的LinearGradBrush包含一个确实具有此类属性的(私有)内部类,但是属性编辑器无法知道向该内部类的哪个实例分配属性值。 (The property editor might not even be able to see this inner class, given that it is private.) (如果属性编辑器是私有的,则属性编辑器甚至可能看不到该内部类。)

I recommend that you move this property out of your inner class and get rid of the class. 我建议您将此属性移出内部类并摆脱该类。 I cannot honestly see a reason why you would need to use an inner class here. 老实说,我看不到为什么需要在这里使用内部类的原因。

Also, I'd like to point out that your dependency property isn't declared correctly. 另外,我想指出的是,您的依赖项属性未正确声明。 It doesn't use the correct naming conventions, and I cannot see any call to the BackgroundBrush() method which initialises the dependency property. 它没有使用正确的命名约定,而且我看不到对初始化依赖项属性的BackgroundBrush()方法的任何调用。 I would expect the property to be declared as follows (note the name of the field, the fact that it is readonly and that the first parameter of the Register method is the name of the property): 我希望该属性的声明如下(请注意字段的名称,该属性是readonly并且Register方法的第一个参数是该属性的名称):

public static readonly DependencyProperty BackgroundProperty =
    DependencyProperty.Register("Background", typeof(Brush), typeof(LinearGradBrush));

[Description("CuloareBG"), Category("Z")]
public Brush Background
{
    get { return (Brush)GetValue(BackgroundProperty); }
    set { SetValue(BackgroundProperty, value); }
}

I made this change to your code, switched to another XAML page (ie not LinearGradBrush.xaml ), and added your control to this XAML page as an element <local:LinearGradBrush /> . 我对您的代码进行了更改,切换到另一个XAML页面(即,不是LinearGradBrush.xaml ),并将您的控件作为元素<local:LinearGradBrush />添加到了此XAML页面。 While the text cursor was over this element, the Background property appeared in the Z category of the properties window. 当文本光标位于该元素上时, Background属性将出现在属性窗口的Z类别中。

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

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