C#WPF附加属性 - 错误:“XML名称空间中不存在该属性”

时间:2013-03-14 13:21:25

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

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

为例

一切都很好,但是当我试图在某些控件上使用它时,我得到了以下错误...

  

错误1 XML命名空间'clr-namespace中不存在'DependencyPropertiesHoster.AcessLevel'属性:ImageGUI.App_Code; assembly = ImageGUI'。第131行位置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);
        }
    }
}

}

这是我在Window的声明

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

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

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

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

提前致谢。

1 个答案:

答案 0 :(得分:14)

感谢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);

谢谢,并希望它在将来帮助某人。