繁体   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