Click here to Skip to main content
15,889,595 members
Articles / Desktop Programming / WPF
Tip/Trick

Binding with Properties defined in Code Behind

Rate me:
Please Sign up or sign in to vote.
4.83/5 (5 votes)
7 Jul 2013CPOL2 min read 33.1K   11   5
An easy way to bind XAML with properties defined in code behind

Introduction

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.

Background

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.

Solution

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.

C#
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

Example:

XML
<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.

License

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


Written By
Software Developer (Senior)
India India
http://stackoverflow.com/users/1479080/amit-mittal

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
XtErMiNaToR1029-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
Ankush 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.