有没有办法简化WPF和Silverlight中依赖项属性的设置?

WTL*_*bie 6 silverlight wpf wpf-controls

我有一个基本的WPF/Silverlight用户控制代码,其中包含一个标签,我想设置使用该控件的代码的值.有没有办法简化依赖属性和相关事件的定义要求?看似简单的编码任务(属性,方法和相关布线)似乎非常嘈杂.

    private static DependencyProperty CountProperty;

    public MyWpfUserControl()
    {
        InitializeComponent();
        PropertyChangedCallback countChangedCallback = CountChanged;
        var metaData = new PropertyMetadata(countChangedCallback);
        CountProperty = DependencyProperty.Register("Count", typeof (int), typeof (MyWpfUserControl), metaData);
    }

    public int ItemsCount
    {
        get { return (int) GetValue(CountProperty); }
        set { SetValue(CountProperty, value); }
    }

    private void CountChanged(DependencyObject property,
                              DependencyPropertyChangedEventArgs args)
    {
        // Set the value of another control to this property
        label1.Content = ItemsCount;
    }
Run Code Online (Sandbox Code Playgroud)

Ric*_*key 1

您确信依赖属性是丑陋且难以使用的。事实上,在上面的代码示例中,甚至存在错误!你需要打电话给医生——WPF 医生!

以下是 WPF 博士针对您想要的所有依赖属性风格的片段:

他的网站上也有视频显示他使用它们。老实说,我自己不使用它们,但我一直想尝试一下。我确实使用内置的片段。