Pergunta

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.

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top