簡體   English   中英

對依賴項屬性XAML使用綁定

[英]Using bindings for dependency property XAML

為另一個依賴屬性問題表示歉意

遵循以下問題:

依賴項屬性的XAML綁定

只能在DependencyObject的DependencyProperty上設置“綁定”

而本教程:

http://wpftutorial.net/DependencyProperties.html

我有個類似的問題。 嘗試解決方案后,變化不大。

我已經制作了一個控件庫:WpfCustomControlLibrary1

 public class CustomControl1 : Control
{



    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
    }

並添加了具有必需回調的依賴項屬性:

FrameworkPropertyMetadata meta = new FrameworkPropertyMetadata(null,FrameworkPropertyMetadataOptions.Inherits, PropertyChangedCallbackMethod, OnDictionaryCoerce,false);

    public static readonly DependencyProperty DictionaryProperty = DependencyProperty.Register("DictionaryProperty",typeof(Dictionary<string,int>),
        typeof(CustomControl1),new FrameworkPropertyMetadata(null,FrameworkPropertyMetadataOptions.Inherits));

    private Dictionary<string, int> _Dictionary;

    public static void PropertyChangedCallbackMethod(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // I am not entirely sure what code should be implemented here, leaving it blank for now
    }

    private static Object OnDictionaryCoerce(DependencyObject sender,Object data)
    {
        return data;
    }

    private static bool OnValidate(object data)
    {
        return data is Dictionary<string, int>;
    }
    public Dictionary<string, int> Dictionary
    {
        get
        {
            return _Dictionary;
        }
        set
        {
            _Dictionary = value;

        }
    }

}
}

然后,我嘗試將屬性設置為應用程序中的綁定,但出現錯誤提示:

錯誤

如何將我的依賴屬性設置為依賴對象? 還是他們設置綁定到依賴屬性的方法?

依賴屬性的基類不是依賴對象嗎?

用正確的名稱注冊DP:

DependencyProperty.Register("Dictionary", ...

然后在DP包裝器屬性中使用GetValueSetValue方法

public Dictionary<string, int> Dictionary
{
    get
    {
        return (Dictionary<string, int>)GetValue(DictionaryProperty);
    }
    set
    {
        SetValue(DictionaryProperty, value);
    }
}

private Dictionary<string, int> _Dictionary; 字段不是必需的

暫無
暫無

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

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