Dependency Property in WPF

江枫思渺然 提交于 2020-01-30 11:12:52

问题


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>

回答1:


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" />



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/13802245/dependency-property-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!