.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

How and When Dependency Properties are bound and evaluated?

Posted By:      Posted Date: October 26, 2010    Points: 0   Category :WPF


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

View Complete Post

More Related Resource Links

Dependency Properties

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

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?

[Custom control] Dynamically evaluated properties?


There's a control with propertygrid-alike UI, accepting IDictionary with a known keys set (let it be the SqlConnectionStringBuilder for example. Actually, it is a custom class).

I do not wish to specify key values in template bindings (e.g. Text = "{Binding SourceProp[keyName], RelativeSource={RelativeSource TemplatedParent}}"), so I've added set of protected properties and bind them to the SourceProp property

    public CustomControl()
        new Binding("SourceProp[keyName]")
          Mode = BindingMode.TwoWay,
          Source = this

TextBox shows "old" value after being Coerced when bound to a Dependency Property




I am writing a WPF Custom Control.  I want to be able to limit the input for the DependancyProperty "Text".  I have the OnCoerce event handlers connected and this property is bound to the "Text" property on a TextBox in my content template as shown below:


Code Snippet

<TextBox Name="PART_txtEdit"

Background="{TemplateBinding Background}"

BorderBrush="{TemplateBinding BorderBrush}"

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="<

TriggerAction's properties not bound when used with Loaded EventTrigger in a dialog


I have a view that is being used as the Content property in a Window shown with ShowDialog (though Show produces the same problem)

The view accepts its view model in its constructor and assigns it to its DataContext

I am using Blend's interaction EventTrigger on the Loaded event to call a custom TriggerAction that has a Command dependency property bound to an ICommand property of the view model mentioned above.

The problem is that, while the action's Invoke method is called, the Command property hasn't been bound and so the property is null.

However, if I setup a custom event handler for Loaded, I can access the DataContext and the source of the bound command is not null and I can call it successfully. The problem only occurs when going via the event trigger.

In addition, if I add the view to a region within an existing view the action is called and the command is bound correctly, so it seems restricted to views that are the root content of a window.

NOTE: I'm using Prism but I don't think that's related to the problem


Create user control fields based on bound properties



I am creating a user control that I would like to be able to generate the form entry fields based on what custom data object the form is bound to. Basically, I have a drop-down box that allows the user to pick what type of data object they are working with (user, address, etc.) That choice will bind the user control to the related data object. No big deal there. I am stuck, however, on how to generate the form fields based on the properties of the object. I feel like I have seen examples of this in WPF but am not able to find them now. All form fields will be simple label, textbox combinations in a stackpanel.

I know more detial will probably be required. Let me know what specifically you would like to know. Any help of direction is greatly appreciated.



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.

Format Generic List Bound to Gridview


I have a generic list of a custom type. The custom type has a string and double.

I'm binding the list to a gridview and it outputs everything fine.

I'm trying to format the double column into a currency format. It is in a template field as I'm calculating a running total. I pass the value to a function that returns a double and adds the current value to a running total which I will show in the footer.

The format is not changing to currency as expected when using String.Format("{0:c}", GetTotal(Convert.ToDouble(Eval("Amount"))))

Any ideas on what might be going on and how to fix this?


How to take value of bound field of grid in a variable or in a textbox.


How to take value of bound field of grid in a variable or in a textbox.

 I m using this code in .aspx page for grid.

<asp:GridView ID="Grd_Mprocess" runat="server" AutoGenerateColumns="False"

                      Width="100%" AllowPaging="True"


                      AllowSorting="True" PageSize="5"  >

                 <RowStyle CssClass="odd" />


<asp:BoundField HeaderText="DOC_TYPE_PK" DataField="DOC_TYPE_PK"  Visible="true" >


  <asp:TemplateField HeaderText="DOC TYPE ID" Visible="true">  &nbs

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

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