【问题标题】:Dependency Property in the PropertyWindow of VisualStudioVisual Studio 属性窗口中的依赖属性
【发布时间】:2012-09-25 16:29:35
【问题描述】:

首先我会给你我的代码,然后我会问我的问题。

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

如您所见,我有一个 UserControl,其中包含一个类。我的问题是为什么我在控件的“属性”窗口(UserControl.Xaml 的右侧)中看不到带有画笔的类别 Z。

【问题讨论】:

    标签: c# wpf wpf-controls dependency-properties


    【解决方案1】:

    为什么我在控件的“属性”窗口(UserControl.Xaml 的右侧)中看不到带有画笔的类别 Z。

    很简单,因为您的LinearGradBrush 不包含使用Z 类别注释的属性。

    您的LinearGradBrush 包含一个(私有)内部类,它确实具有这样的属性,但属性编辑器无法知道该内部类的哪个实例将属性值分配给。 (属性编辑器甚至可能看不到这个内部类,因为它是私有的。)

    我建议您将此属性移出您的内部类并摆脱该类。老实说,我看不出你需要在这里使用内部类的原因。

    另外,我想指出您的依赖属性未正确声明。它没有使用正确的命名约定,而且我看不到对初始化依赖属性的BackgroundBrush() 方法的任何调用。我希望属性声明如下(注意字段的名称,它是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); }
    }
    

    我对您的代码进行了此更改,切换到另一个 XAML 页面(即不是 LinearGradBrush.xaml),并将您的控件作为元素 <local:LinearGradBrush /> 添加到此 XAML 页面。当文本光标在该元素上时,Background 属性出现在属性窗口的Z 类别中。

    【讨论】:

    • 非常感谢,对我帮助很大:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多