0

I am working on a DependencyProperty for my Avalon dock controller. Here is some sample code which i have currently working on.

Requirement is: Create all dependency properties in one single class and access the property in View. Something like this.

<Button isPaneVisible="true"> or <Button isPaneVisible="{Staticresource path=name, Mode=twoway">

Could you please help me to reslove this issue?

namespace Avatar.UI.ViewModel
{
    internal class DependencyPropertyClass : DependencyObject
    {
        public static readonly DependencyProperty IsPaneVisibleProperty =
            DependencyProperty.RegisterAttached("IsPaneVisible", typeof(bool), typeof(DependencyPropertyClass),
                new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsPaneVisible_PropertyChanged));

        private static void IsPaneVisible_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        /// <summary>
        /// Sets the IsPaneVisible for an element.
        /// </summary>
        public bool IsPaneVisible
        {
            get { return (bool)GetValue(IsPaneVisibleProperty); }
            set
            {
                SetValue(IsPaneVisibleProperty, value);
            }
        }

    }
}

<UserControl x:Class="Avatar.UI.View.ContentView"             
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
             mc:Ignorable="d" 
             xmlns:avalonDock="http://avalondock.codeplex.com"     
             xmlns:local="clr-namespace:Avatar.UI.ViewModel"             
             d:DesignHeight="300" d:DesignWidth="300">


<Button IsPaneVisible="true"></Button 

</UserControl>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Nitheen Rao
  • 210
  • 2
  • 11
  • 1
    You ask us to help you to solve the issue, but there is no issue described in your question. Please specify precisely what you expect, and what you get – Steve B Dec 10 '12 at 13:52
  • 2
    seems like you are trying to use a DependencyProperty as attached Property , see http://stackoverflow.com/questions/7148946/attached-properties for explantion – ZSH Dec 10 '12 at 13:53
  • wild guess: Don't make the class internal might be problematic for the XAML parser to use reflection on an internal class. – dowhilefor Dec 10 '12 at 14:51
  • Remember to accept an answer or to post your own. This will help you to get answers to your future questions (as your "accept" rating is posted alongside your user ID) – JDB Dec 10 '12 at 21:48

3 Answers3

3

Defining an attached dependency property also requires the definition of static get and set accessor methods. See Custom Attached Properties for more information. Note also that your class does not necessarily need to be derived from DependencyObject as long as it only defines attached properties. But it is always a good idea to define such properties in a public class.

public class DependencyPropertyClass
{
    public static readonly DependencyProperty IsPaneVisibleProperty =
        DependencyProperty.RegisterAttached("IsPaneVisible", typeof(bool), typeof(DependencyPropertyClass),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsPaneVisible_PropertyChanged));

    private static void IsPaneVisible_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    }

    public static bool GetIsPaneVisible(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsPaneVisibleProperty);
    }

    public static void SetIsPaneVisible(DependencyObject obj, bool value)
    {
        obj.SetValue(IsPaneVisibleProperty, value);
    }
}

And as Cyborgx37 has pointed out, you would use an attached property in XAML like this:

<Button local:DependencyPropertyClass.IsPaneVisible="True" />
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • After Making above changes, I am getting this error: The local property "IsPaneVisible" can only be applied to types that are derived from "DependencyPropertyClass". – Nitheen Rao Dec 11 '12 at 04:28
  • Did you do it as shown here or as shown by Cyborgx37? His setter is wrong. – Clemens Dec 11 '12 at 12:10
  • Yes, I have tried but no luck. But IsPanelVisible able to access in Main Window not in User control. – Nitheen Rao Dec 11 '12 at 12:14
  • You haven't answered my question. Again, did you do it as shown here or as shown by Cyborgx37? – Clemens Dec 11 '12 at 12:16
  • Guys have you tried to access the Dependency Property defined in user control and accessing it in User control it self?. – Nitheen Rao Dec 11 '12 at 12:25
  • Then I say it again: The setter shown by Cyborgx37 is **wrong**! Write the setter as shown here. – Clemens Dec 11 '12 at 12:25
  • Clemens, Could you please try what said in by yourself. Because I have tried all those option and no luck. Please try to have dependency property in User control code behind and access the same property in user control xmal file. – Nitheen Rao Dec 11 '12 at 12:31
  • I did that already. This attached property works perfectly for me. Maybe you need to read up on attached properties. At least when you say "I am getting this error:..." you have to say exactly where you get that error. You could either edit your question, or even decide to create a new one. – Clemens Dec 11 '12 at 12:37
  • And when you say "have dependency property in User control code behind and access the same property in user control xmal file" why don't you just edit your question to show us what exactly you did? – Clemens Dec 11 '12 at 12:38
  • Could you please post the code over here. That will be great. – Nitheen Rao Dec 11 '12 at 12:39
  • It's your question. So you post what you tried and what didn't work. I can't guess your intended use. – Clemens Dec 11 '12 at 12:41
1

I could be wrong, but I think you are looking for this:

<Button local:DependencyPropertyClass.IsPaneVisible="true"></Button>

You have to specify the namespace, since IsPaneVisible is not part of the "http://schemas.microsoft.com/winfx/2006/xaml/presentation" namespace.

See: Attached Properties Overview

EDIT
It's been a while since I've done this, so things are slowly coming back to me as I scan your code. For an attached property, you cannot use an instance property to get/set the property. You must create static Get<PropertyName> and Set<PropertyName> functions:

public static void SetIsPaneVisible(DependenyObject target, Boolean value)
{
    target.SetValue(IsPaneVisibleProperty, value);
}
public static bool GetIsPaneVisible(DependenyObject target)
{
    return (bool)target.GetValue(IsPaneVisibleProperty);
}

Seriously... please read the linked article. It's all explained there.

JDB
  • 25,172
  • 5
  • 72
  • 123
  • xmlns:local="clr-namespace:Avatar.UI.ViewModel" this is namespace i am using. – Nitheen Rao Dec 10 '12 at 14:00
  • Updated my answer. You need to use the class name too, I think. – JDB Dec 10 '12 at 14:33
  • Also, it says the property is not belongs to dependency object. – Nitheen Rao Dec 10 '12 at 14:35
  • After Making above changes, I am getting this error: The local property "IsPaneVisible" can only be applied to types that are derived from "DependencyPropertyClass". – Nitheen Rao Dec 11 '12 at 04:28
  • The setter shown here is wrong. It should call `target.SetValue(IsPaneVisibleProperty, value)` instead of `element.SetValue(IsIsPaneVisible, target)`. Also the getter should read `target.GetValue(IsPaneVisibleProperty)`. – Clemens Dec 11 '12 at 12:11
  • Still wrong: `target.SetValue(IsPaneVisibleProperty, target)` should be `target.SetValue(IsPaneVisibleProperty, ` **`value`** `)`. – Clemens Dec 11 '12 at 15:06
0

The dependency property should derive the base calss for which you are going to create dependency property. for example, if you are going to create dependency property for button, then derive the base class button to your class.

This is how I have resolved my issue.

Nitheen Rao
  • 210
  • 2
  • 11