Solved

Dependency Property vs Attached Property - WPF

Posted on 2011-03-10
3
811 Views
Last Modified: 2012-06-21
Hi,

I have the following statement:

Attached properties differ from normal dependency properties in that they are not wrapped in CLR properties (i.e. standard .NET properties).

Dependency Properties Example:

<Canvas>
                   <Rectangle Height="72" Width="131" Canvas.Left="74" Canvas.Top="77" />
</Canvas>

Attached Properties Example:

<DockPanel>
      <Button x:Name="Button1" DockPanel.Dock="Left" />
</DockPanel>

Can you please let me know what is the difference between these two ? It looks like both are same but i am missing something !

Thanks
0
Comment
Question by:milani_lucie
  • 2
3 Comments
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
The basic difference is that Attached Properties are dependency properties defined in a parent control and attached to a child control.  In your sample Canvas.Left is a property of Canvas, but it's attached to Rectangle.

More here:

Dependency Properties Overview
http://msdn.microsoft.com/en-us/library/ms752914.aspx

Attached Properties Overview
http://msdn.microsoft.com/en-us/library/ms749011.aspx
0
 

Author Comment

by:milani_lucie
Comment Utility
wdosanjos:

Can you please define in simple terms:

What are Attched Properties ? What are Dependency Properties ? As per your definition, Attached Properties are nothing but Dependency Properties but attached to child controls. Am i correct ?

Can be Dependency Properties exist without attaching to child controls ?

Thanks
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
Yes, that's correct. Attached Properties are Dependency Properties that have been attached to child controls.

In the example below, Name, Background, BorderBrush, and BorderThickness are Dependency Properties of Button, while DockPanel.Dock is an Attached Property.

<DockPanel>
<Button Name="mybutton" Background="Green" BorderBrush="Black" BorderThickness="1"
   DockPanel.Dock="Left">
  My Button
</Button>
</DockPanel>

Open in new window


Maybe if you see how that's is done in code, it will make more sense. If you were to create the above dock panel and button in code, it would be something like this:

DockPanel myDockPanel = new DockPanel();
Button myButton = new Button();

// Set Regular Dependency Properties
myButton.Name = "mybutton";
myButton.Content = "My Button";
myButton.Background = Brushes.Green;
myButton.BorderBrush = Brushes.Black;
myButton.BorderThickness = new Thickness(1.0);

// Add Button to DockPanel
myDockPanel.Children.Add(myCheckBox);

// Set Attached Dependency Property (through the parent on the child control)
DockPanel.SetDock(myButton, Dock.Left);

Open in new window


I hope this helps to clarify.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now