Solved

Dependency Property vs Attached Property - WPF

Posted on 2011-03-10
3
818 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
ID: 35099531
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
ID: 35099717
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
ID: 35100758
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Advice on Xojo as a development tool over VB. 4 53
Extracting last characters in a substring 5 26
VB.NET - Error - Object not set in Module 4 33
Store results in vb.net 3 20
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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