C# WPF Attached Properties - Error: "The property does not exist in XML namespace"
Asked Answered
B

1

19

I need to create a new property to existing WPF controls (Groupbox, textbox, checkbox, etc), one that will storage its acess Level, therefore I've found out Attached Properties. I used as example this site http://dotnetbyexample.blogspot.com.br/2010/05/attached-dependency-properties-for.html

Everything was fine, but then I got the following error when trying to use it on some control...

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

This is my C# code snippet...

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

Here is my declaration at the Window

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

And here is my usage of the property (where the error lies...)

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

Observation: EAcessLevel is just a simple enumerator.

Thanks in advance.

Bambino answered 14/3, 2013 at 13:21 Comment(5)
Are you sure that AcessLevel is in the assembly ImageGUI? Does it work if you remove the assembly=ImageGUI part of your declaration?Alicia
+1 for someone finally including sufficient information in a SO question. To add to @Bob's comment, if your attached property (or whatever) is in the same assembly as your XAML, you shouldn't include the assembly= part of your XML->CLR namespace mapping.Preponderate
Thank you! I can't believe it was that simple, but I'm glad it was. Just removing the ending part has fixed it (assembly=...). But I wonder how do I acess this property in code? How would I retrieve it programatically now? example of perfect scenario: EAcessLevel controlAcess = gbApplications.AcessLevel;Bambino
@Luishg, please post it as an answer to your own question - it is a good practice to have an answer separated from the question (and prevent this question to reappear in the unanswered questions list - as a bonus ;).Philipp
Thanks, it's done. Thank you guys for the help and advices.Bambino
B
15

Thanks Bob and Kent for the answers, that pretty much solved the issue. In this scenario, just changing

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

to

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

fixed the situation. Everything else was correct.

Regarding my other comment about how to retrieve the specified value, it would be like that:

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

Thanks, and hope it also helps somebody in the future.

Bambino answered 9/7, 2013 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.