Solved

Dependency Property vs Attached Property - WPF

Posted on 2011-03-10
3
823 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

615 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