簡體   English   中英

WPF中的依賴項屬性

[英]Dependency Property in WPF

我正在為我的Avalon擴展塢控制器開發DependencyProperty 這是我目前正在處理的一些示例代碼。

要求是:在一個類中創建所有依賴項屬性,然后在View中訪問該屬性。 這樣的事情。

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

您能幫我解決這個問題嗎?

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>

定義附加的依賴項屬性還需要定義靜態的get和set訪問器方法。 有關更多信息,請參見自定義附加屬性 還請注意,您的類不必僅從DependencyObject派生,只要它定義了附加屬性即可。 但是在公共類中定義此類屬性始終是一個好主意。

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

正如Cyborgx37指出的那樣,您將在XAML中使用如下附加屬性:

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

我可能是錯的,但是我認為您正在尋找:

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

您必須指定名稱空間,因為IsPaneVisible不屬於“ http://schemas.microsoft.com/winfx/2006/xaml/presentation”名稱空間。

請參閱: 附加屬性概述

編輯
自從我這樣做已經有一段時間了,所以當我掃描您的代碼時,事情慢慢回到我身邊。 對於附加屬性,您不能使用實例屬性來獲取/設置該屬性。 您必須創建靜態的Get<PropertyName>Set<PropertyName>函數:

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

認真...請閱讀鏈接的文章。 一切都在那里解釋。

依賴項屬性應派生要為其創建依賴項屬性的基本指標。 例如,如果要為button創建依賴項屬性,則將基類button派生到您的類。

這就是我解決問題的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM