C# WPF 附加属性 - 错误 : "The property does not exist in XML namespace"

标签 c# wpf dependencies dependency-properties attached-properties

我需要为现有的 WPF 控件(组框、文本框、复选框等)创建一个新属性,该属性将存储其访问级别,因此我找到了附加属性。 我以这个网站为例 http://dotnetbyexample.blogspot.com.br/2010/05/attached-dependency-properties-for.html

一切都很好,但是当我尝试在某些控件上使用它时出现以下错误...

Error 1 The property 'DependencyPropertiesHoster.AcessLevel' does not exist in XML namespace 'clr-namespace:ImageGUI.App_Code;assembly=ImageGUI'. Line 131 Position 97. ImageGUI\MainWindow.xaml 131 97 ImageGUI

这是我的 C# 代码片段...

namespace ImageGUI.App_Code
{    
    public static class DependencyPropertiesHoster
    {
        //[AttachedPropertyBrowsableForChildren]
        public static readonly DependencyProperty AcessLevelProperty =
            DependencyProperty.RegisterAttached(
                "AcessLevel",
                typeof(EAcessLevel),
                typeof(DependencyPropertiesHoster),
                new PropertyMetadata(AcessLevelChanged)
            );

        // Called when Property is retrieved
        public static EAcessLevel GetAcessLevel(DependencyObject obj)
        {
            if (obj != null)
                return (EAcessLevel)obj.GetValue(AcessLevelProperty);
            else
                return EAcessLevel.Client;
                //return obj.GetValue(AcessLevelProperty) as EAcessLevel;            
        }

        // Called when Property is set
        public static void SetAcessLevel(DependencyObject obj, EAcessLevel value)
        {
            obj.SetValue(AcessLevelProperty, value);
        }

        // Called when property is changed
        private static void AcessLevelChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            var attachedObject = sender as UIElement;
            if (attachedObject != null)
            {
                // do whatever is necessary, for example
                // attachedObject.CallSomeMethod(                 
                // args.NewValue as TargetPropertyType);
            }
        }
    }
}

这是我在窗口的声明

xmlns:CustomDepen="clr-namespace:ImageGUI.App_Code;assembly=ImageGUI"

这是我对该属性的用法(错误所在...)

<GroupBox Name="gbApplications" Header="{DynamicResource applications}" CustomDepen:DependencyPropertiesHoster.AcessLevel="Client">

观察:EAcessLevel 只是一个简单的枚举器。

提前致谢。

最佳答案

感谢 Bob 和 Kent 的回答,这几乎解决了问题。 在这种情况下,只需更改

xmlns:CustomDepen="clr-namespace:ImageGUI.App_Code;assembly=ImageGUI"

xmlns:CustomDepen="clr-namespace:ImageGUI.App_Code"

解决了这个问题。 其他一切都是正确的。

关于我关于如何检索指定值的其他评论,它会是这样的:

EAcessLevel currentAcess = (EAcessLevel)gbApplications.GetValue(DependencyPropertiesHoster.AcessLevelProperty);

谢谢,希望它也能帮助将来的人。

关于C# WPF 附加属性 - 错误 : "The property does not exist in XML namespace",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15410463/

相关文章:

c# - 从 Dispose() 触发事件是否可以?

c# - 如何使用 JSON.Net 获取自引用 C# 实体来序列化子项?

c# - 在基于 View 的WPF应用程序中从ViewModel更改 View

maven - 将 Maven 输出发送到文件

scala - sbt 构建后 gephi-toolkit 中缺少类

c# - 代码中的访问冲突不是我的

c# - Visual C# 回溯 : how to know where [external code] resides?

wpf - 条件 XAML

wpf - 覆盖 ControlTemplate 中的属性

maven - 如何通过Gradle将所有依赖项复制到Maven存储库?