Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dependency Property vs Attached Property - WPF

Posted on 2011-03-10
3
Medium Priority
?
832 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline

877 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