Solved

Dependency Property vs Attached Property - WPF

Posted on 2011-03-10
3
819 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 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