Wednesday, April 18, 2007

Dependency Properties

I have faced some interesting concept in WPF ,That one is Dependency Properties ,Its really sound cool.But it takes full day for me to get the complete idea. First I have tried to get the idea from some books.but I couldn't , Then I have visited some sites about DP. There i got some partial idea. Then again Book with couple of samples, Finally I got..

Its really interesting concepts in WPF.

The concept of dependency properties(what i have understood):


WPF contain window Element Tree. It consists of all the visual objects created by the program .For example iam creating 3 buttons and put them into the Grid ,then Grid into the Window. Here Window is the parent node ,grid is child node of window and buttons are child of Grid.

Visual Tree (Not a Logical Tree because Logical Tree contain all the elements like GridRowDefenitions , GridColDefenitions and Run object associated with text Block,etc..). Obviously this is not a complete visual tree because in visual tree may include visual objects that the program does not explicitly create. (E
x. Border,Background,etc..)


Here we have to take FontSize as a Dependency Property.

In WPF, if I try to change the FontSize of the window , It will change all the child elements fontsize automatically until the child element fontsize changed by explicitly, If we changed the fontsize of child element by explicitly it won't affect the original fontsize of that particular child element. But rest of the elements will get affect. This is called Dependency Properties.The interesting aspects is, In this visual tree Grid don't have a font size property even though the buttons font size will get changes,

Using this concepts we can create our own dependency properties for our custom controls. But our control class must be inherited by FrameWorkElement or UIElement class. Here fontsize isn't the only property that works like this. Allow-Drop ,IsEnabled,IsVisible,etc.. These are all UIElement class properties.


If you got! enjoy and leave your comments here!!!!!!!!!!!!!!!!!!!

3 comments:

  1. Gr8 .Keep up posting..Best wishes..

    ReplyDelete
  2. thnx!!!
    Grt explanation..
    got the core idea in 5 mins!!

    ReplyDelete
  3. hi


    can you tell me about button click events,
    Is it Possible when we double click on the Control, we can see the event in code behind page. like that in wpf.

    or How can we write the code for button click.

    ReplyDelete