Click here to Skip to main content
12,950,268 members (67,687 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 28 Jun 2013

Binding with Properties defined in Code Behind

, 7 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
An easy way to bind XAML with properties defined in code behind


This is a little trick I use to bind XAML stuff with code behind properties. This tip/trick will allow you to easily achieve such a binding.


There are a number of ways using which you can bind code-behind properties. For e.g., assigning a name to the code-behind object and then using ElementName in the data binding expression, using relative source with condition on ancestor type, etc. This text does not cover these ways.

Without going into the goodness or badness of the above methods, I would like to point out that I sometimes find these methods unpredictable/cumbersome. For e.g., I was recently hit with the issue described here: MSDN Link.

Using element name also does not work sometimes due to the same reason as described in the above MSDN link.

Long story short, using the above methods is not always applicable. Also given that bindings don't 'error out' (read exceptions), it can become notoriously hard to detect the root cause of the problem.


If we can obtain a reference to the current code-behind object, our job becomes easy. All we have to do is either set DataContext for the binding to the obtained reference or directly use the obtained reference as source of the binding. Also, the mechanism to obtain this reference should be usable from XAML.

Initially, I tried with attached properties, as attached properties get a reference to the object to which it is attached. All I had to do was, retain a reference to the object it is attached to and then return it through a read-only property (this solution is not presented here).

However, later I discovered that all this was quite unnecessary. I could have easily used a nice little Markup Extension. Nothing fancy, but just a few lines of code.

public class RootExtension : MarkupExtension
       public override object ProvideValue(IServiceProvider serviceProvider)
           IRootObjectProvider provider = serviceProvider.GetService
           (typeof(IRootObjectProvider)) as IRootObjectProvider;
           if (provider != null)
               return provider.RootObject;

           return null;

How Does It Work

Markup Extensions are evaluated in the context of the XAML document they are used in. The above extension simply finds and returns the root object of current XAML document or in other words, the code-behind object.

How To Use It


<DataGridTemplateColumn Width="{Binding Source={local:Root}, 
Path=ColumnOneWidth}" CellTemplate="{Binding Source={local:Root}, Path=ColumnOneTemplate}"/>

where ColumnOneWidth and ColumnOneTemplate are properties defined in the code-behind of the user control above XAML fragment forms part of.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi27-Jul-13 22:12
professionalAmir Mohammad Nasrollahi27-Jul-13 22:12 
GeneralRe: My vote of 4 Pin
Amit_Mittal28-Jul-13 23:18
professionalAmit_Mittal28-Jul-13 23:18 
GeneralMy vote of 5 Pin
XtErMiNaToR1029-Jul-13 1:54
memberXtErMiNaToR1029-Jul-13 1:54 
GeneralRe: My vote of 5 Pin
Amit_Mittal9-Jul-13 17:21
professionalAmit_Mittal9-Jul-13 17:21 
GeneralMy vote of 5 Pin
Ankush Bansal28-Jun-13 0:53
memberAnkush Bansal28-Jun-13 0:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 8 Jul 2013
Article Copyright 2013 by Amit_Mittal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid