WPF/SilverLight 中的依赖属性 [英] Dependency Property In WPF/SilverLight

查看:28
本文介绍了WPF/SilverLight 中的依赖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索了关于如何开始使用 WPF/silverlight 中使用的依赖属性,但对依赖属性没有任何概念,任何人都可以从初学者的角度告诉我,以便我对它有所了解并在我的项目中使用它

I have searched on google about how to get started with the dependency property used in WPF/silverlight but didn't get any idea of the dependency property, can any one tell me about it , from beginner point of view, so that I get some idea about it and use it in my project

提前致谢.

谁能给我简单应用程序的链接或代码示例,以简单的方式解释什么是依赖属性?我会很感激

Can any one give me link or code example of simple application which explain in simple manner what is dependency Property is ??? I will be very thankfull

推荐答案

我发现实现 DependencyProperty 通常涉及四个部分:

I find that implementing a DependencyProperty often involves four parts:

  • DependencyProperty 本身
  • 带有 get 和 set 的属性
  • 静态更改处理程序
  • 实例更改处理程序

您可以向 UserControl 添加依赖项属性,以便您可以将数据绑定到实例化 UserControl 的 DataContext 中的某些内容.例如,您可以向 SoUserControl 添加一个属性:

You can add a dependency property to a UserControl so that you can data bind to something in the DataContext where the UserControl is instantiated. For example you could add a property to a SoUserControl:

    #region SampleProperty // Demo for SO 2424526

    public static readonly DependencyProperty SamplePropertyProperty
        = DependencyProperty.Register("SampleProperty", typeof(int), typeof(SoUserControl), new PropertyMetadata(OnSamplePropertyChanged));


    /// <summary>
    /// Demo for SO 2424526
    /// Gets or sets dependency property.
    /// </summary>
    public int SampleProperty
    {
        get { return (int)GetValue(SamplePropertyProperty); }
        set { SetValue(SamplePropertyProperty, value); }
    }

    /// <summary>
    /// Handld changes to SamplePropertyProperty by calling a handler in the associated object.
    /// </summary>
    /// <param name="obj">object the property is associated with</param>
    /// <param name="e">details of change</param>
    static void OnSamplePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        (obj as SoUserControl).OnSamplePropertyChanged(e);
    }

    /// <summary>
    /// Handle changes to the SamplePropertyProperty dependency property.
    /// </summary>
    /// <param name="e">details of change</param>
    private void OnSamplePropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        int SamplePropertyNewValue = (int)e.NewValue;
        // do something with the internal logic of the control
    }

    #endregion

这篇关于WPF/SilverLight 中的依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆