WPF/SilverLight中的依赖属性

Asi*_*jad 3 silverlight wpf

我在google上搜索了如何开始使用WPF/silverlight中使用的依赖属性,但是没有了解依赖属性,从初学者的角度来看,任何人都可以告诉我它,以便我得到一些关于它的想法并在我的项目中使用它

提前致谢.

任何人都可以给我简单的应用程序的链接或代码示例,以简单的方式解释什么是依赖属性是??? 我会非常感激的

Dou*_*son 7

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

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

您可以向UserControl添加依赖项属性,以便您可以将数据绑定到实例化UserControl的DataContext中的某些内容.例如,您可以向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
Run Code Online (Sandbox Code Playgroud)