.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Dependency Properties

Posted By: Amit Mehra     Posted Date: April 08, 2010    Points: 2   Category :WPF
When you begin to develop appliations with WPF, you will soon stumble across DependencyProperties. They look quite similar to normal .NET properties, but the concept behind is much more complex and powerful

View Complete Post

More Related Resource Links

Foundations: Dependency Properties And Notifications


Windows Presentation Foundation dependency properties don't always play well with others. Learn how you can compensate for their lack of notification events.

Charles Petzold

MSDN Magazine September 2008

Dependency Properties get/set method question



I recently added a new dependency property to a control of mine and bound its value to my control's ViewModel.  I put custom code in the Get method of the dependency property and saw the return value from that method does not come across to the ViewModel.  After some research, it seems that when the XAML parser attempts to get or set this property's value that it does not use these get/set methods at all.  So to make this DP useful for my scenario, I have to figure out another way to set this value ahead of time, i.e. through code.

My question is, what is the point of these get/set methods for your DP if the XAML parser isn't going to use them?  Is it really all that useful to use these in code?  I would imagine DP's are mostly being used for the purpose of data binding in which case it'd be helpful to be able to define how WPF will get and set the value.  I liken this scenario to a public property with a private backing field, and having WinForms or whatever just directly access the backing field and skipping my property's getter/setter, which would obviously be a bug.

I'd appreciate any insight on this.

How do I bind these dependency properties?


I have two custom classes: "Foo" with a dependency property called "FooValue", and "Bar" with a dependency property called "BarValue". I can declare these in Xaml and bind them the usual way like so:

	<my:Foo x:Name="foo" x:Key="foo" FooValue="Hello World!">
	<my:Bar x:Key="bar" BarValue="{Binding Source={StaticResource foo},Path=FooValue}"/>

But what if Bar is a member of Foo? How do I do the binding then?

<my:Foo x:Name="foo" x:Key="foo" FooValue="Hello World!">
		<my:Bar BarValue="{Binding *** what goes here? ***}"/>

I can't bind to the "foo" key because the parent object is still in the process of being created and doesn't exist yet. I've also tried using FindAncestor but that doesn't seem to work with custom classes. What's the mechanism that controls use to get it to work?

How and When Dependency Properties are bound and evaluated?



I have a custom combo box control derived from existing WPF combo box. This custom combo has a DP called AssociatedEnum, once set it evaluates the enum menbers and adds them to Items collection.. However all this enumerating over available values, is done in Combo's loaded event Is it reasonable to assume that all the bindings on Dependency props will be set and evaluated to their initial values during the controls load event.

I am not very sure about this as i have seen some instances where the Binding is evaluated post load event, which defeats entire purpose of writing the selection in load event. My question should i go ahead and implement property changed handler for this dependency property??

Regards, v703218

link | edit |

Can you add Dependency Properties to custom activities?


I'm trying to add a simple DependencyProperty to a base activity of mine that inherits from NativeActivity.  Is this possible?  It doesn't seem so since the SetValue and GetValue methods of the dependency property definition are not recognized.

I want a dependency property because I'd like to be able to check the property from a Trigger in the activity designer ... like this:

            <Trigger Property="{Binding Path=ModelItem.IsEnabled}" Value="True">
              <Setter TargetName="Header" Property="<

WCF Tip: Using Properties in Service Contracts

you'll notice all of the Service Contracts are implemented with interfaces containing only methods. For the longest time I thought that's all that was supported, until I thought about it a little more. In C# land, properties are simply wrappers around a get method and set method, so why wouldn't we be able to use properties in a service contract? Well the answer is we can.

Automatic vs Explicit Properties

If the reason that motivated the change from automatically implemented property to explicitly implemented property was to change the semantics of the property then you should evaluate whether the desired semantics when accessing the property from within the class are identical to or different from the desired semantics when accessing the property from outside the class.

Xml File Methods and Properties

Method and Properties of XmlTextWriter class:
The XmlWriter class is a base class that outputs an XML data structure. It has methods to sequentially write XML elements in a proper hierarchy to create an XML file or other types of output streams. The System.Xml namespace provides support for the XmlWriter. It must be imported to the ASP.NET page with the directive

<%@ Import Namespace="System.Xml" %>

The XmlTextWriter

XmlWriter is an abstract class that is implemented as an extended XMLTextWriter class. The XmlTextWriter writes a sequence of XML nodes one element at a time. Output can be written to an XML file, although other streams of output data can be produced. It can create a new file or overwrite an existing file. Its non-cached writing techniques provides very fast generation of an output stream.

C# conversion to VB and properties


I am trying to implement image caching for a web application and I found some code on the Internet that implements an HttpHandler that does this. The code was in C# and since my web app is written in VB, I decided to convert the code to VB. I managed to convert everything except the following code (in bold):

public class FileExtensionCollection : ConfigurationElementCollection
        public override ConfigurationElementCollectionType CollectionType
                return ConfigurationElementCollectionType.AddRemoveClearMap;

        public FileExtension this[int index]
            get { return (FileExtension)BaseGet(index); }

using reflection to emit properties to the top of the entity graph


I've got a class

  public class Criminal

public HouseholdMember[] LiedAboutFSHousehold { get; set; }

      public HouseholdMember[] FoodstampTrafficingHousehold { get; set; }
HouseholdMember inherits Person, as do many other classes.  

  public class HouseholdMember: Person


What I&#

Inside Microsoft patterns & practices: Dependency Injection in Libraries


This article discusses how to write a library or framework that uses the Dependency Injection pattern and how the change in focus affects the usage of the pattern.

Chris Tavares

MSDN Magazine November 2009

{ End Bracket }: Dependency Management


Dependency management, in which one group provides a software component to be utilized by others, can really benefit from some structure and collaborative efforts.

Eric N. Bush

MSDN Magazine August 2007

Advanced Basics: Setting Word Document Properties the Office 2007 Way


The last time I wrote this column (March 2006), I shared an application that allows you to update all the Microsoft® Word documents in a folder and its subfolders. Each time the application finds a document in the specified path, it updates the document properties to match those you specified in the application.

Ken Getz

MSDN Magazine June 2006

Advanced Basics: Set Word Document Properties Programmatically


At the beginning of another lovely day of writing courseware in mad pursuit of unrealistic deadlines, I received a frantic call from a business partner. He was at the end of a long consulting project and had several hundred Microsoft® Word documents, all of which required their document properties to be set identically, except the Title property of the document, which was to be based on the document file name, minus the .

Ken Getz

MSDN Magazine March 2006

Design Patterns: Dependency Injection


Today there is a greater focus than ever on reusing existing components and wiring together disparate components to form a cohesive architecture. But this wiring can quickly become a daunting task because as application size and complexity increase, so do dependencies.

Griffin Caprio

MSDN Magazine September 2005

Deserializing nullable model properties


Hi everyone,

I'm posting Json data to an action that has a model class as a parameter using the JsonValueProviderFactory. Everything works except for any nullable properties in the model, such as double? and bool? - they always materialize as null, even if a value is posted.

I'm not sure if this issue is specific to the JsonValueProviderFactory, or MVC in general.

Being new to MVC I'd appreciate any help with this issue.


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend