views:

125

answers:

4

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

thanks in advance.

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

A: 

You should read a book, for starters.

I have read WPF In Action With Visual Sutdio 2005 from Manning.

As an answer for the negative punctuation just given to me, Dependency Property is part of a bigger plan called WPF and one can't understand Dependency Properties without the basics.

Therefore i think it's a waste of time to try to understand only what is a Dependency Property because i fear one will end up using it incorrectly.

Also, MSDN website has free information about WPF and Dependency Property.

Luis Filipe
A: 

Have a look at Luis Abreu's blog :http://msmvps.com/blogs/luisabreu/

He's got a lot of info there about dependecy properties and how to use them.

Sorskoot
+2  A: 

From my use with Dependency Properties. They become most important when binding. When you bind to display a regular property, the initial binding will work great, however, the UI will not always update when the property changes, in which case you could implement INotifyPropertyChanged on your class and raise a the NotifyPropertyChanged event, but a Dependency Property will update for you without implementing INotifyProperty Changed.

Triggers is another big one. If you wish to create a Trigger to fire off a WPF Animation whenever one of your properties is set to a certain value... then you need to be triggering off of a dependency property.

DepenedencyProperties can only be implemented on types that derive from DependencyObject. UIElement derives from Visual which derives from DependencyObject, so most properties from .Net controls are dependency properties already. And when creating and registering my own DependencyProperties it's usually on my custom UserControls as that inherits from UIElement.

It was very frustrating for me when I first tried to create a dependency property on one of my regular classes (you're better off using INotifyPropertyChanged here)

Links: http://msdn.microsoft.com/en-us/library/ms752914.aspx, http://www.wpftutorial.net/DependencyProperties.html

Scott
+2  A: 

I find that implementing a DependencyProperty often involves four parts:

  • The DependencyProperty itself
  • Property with get and set
  • Static changed handler
  • Instance change handler

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
Doug Ferguson
Thanks for you time and the answer, that is what really need , one think I want to ask you, is how it know that value is changed and it passed to the control to display the changed value.thanks again
Asim Sajjad