WPFix Part 1 (Lambda Converter Extension)

As a mathematician, I always had a fondness for languages. As a programmer, this fondness became a devotion. Some might think that language is just a part of implementation which follows analysis and modeling. I agree that implementation is the least important one. I agree that it is like a tool for implementation, you can choose one or the other. I agree that a separate article, -which does not contain WPF in its header-, is more appropriate for this kind of topic. However, I also believe that programming languages have a much broader effect which includes modeling and even analysis. Our process of thinking is effected by our ways of communication.

Eventually, I will connect this to WPF, somehow in the future. In the last few years, I suppose the number of new programming languages has an exponential growth if we also include the new versions. Some believe that domain specific languages will reproduce asexually, some believe that languages converge to one as they copy their best parts. Even if there is a convergence, there is long way on which we speed up. My guess is that the hypothetical unified programming language, which will be the base of many domain specific languages and will act as a connection between them, will be the way to go. While waiting, I suggest we learn a few of these doomed languages. I hope, we will together witness how less these paths are traveled. I promise that following this post, my technical and theoretical musings will also travel separately. But now, lets start by a few technical ones. C# 3.0 and XAML are two new languages in two different paths. Lets get them together!

I always try to defend WPF in discussions with my colleagues. However, I will try to criticize WPF in this series of articles, namely WPFix. Visual Studio 2008 is to be released by the end of this month but I think, WPF and XAML still have a long way to go. Don’t get me wrong, I am just as glad as you are. I only wish, there was a better way than lengthening the evaluation period to spread the ideas. I will try to keep my critiques precise and search for practical solutions. Hopefully, I will also provide introductory links.


Data binding is pretty much the heart of WPF. We can think of WPF as a pretty complicated converter which consists of little converters for simple data types. However, these converters in the XAML may appear smaller than they are.

This may not be a complete waste of time from code maintenance perspective. But we, mortals, sometimes need a more practical method.


A similar problem in C# has been resolved lately by lambdas. You are no longer have to declare your method which won’t be used anywhere else. Here comes another future article name to my mind, maintenance is overrated. Personal Note: Use more controversial article names than WPFix Part 1. Back to our problem, we need a way to express ourselves in XAML. By express, I mean code. Hence lambda expressions.

Is this too much to ask? Well, this article already took more time than to make the above code work. All I had to do was to put together some existing parts. First of all, we need a markup extension that will construct the converter.

Now, we have a string to parse. Don’t worry, we also have a parser, provided by microsoft. I guess, WPF team and LINQ team do not see each other very much. Take a look at the Dynamic Expression API, which can be found under the samples directory of your VS2008 Beta2 installation. You can also download these samples from this msdn page by following the LINQ samples link. The treasure is inside dynamic.cs source file which also has a nice documentation. Serendipity never leaves me alone.

You can download WPFix1 Solution (VS2008 Beta 2) and start playing. How much longer would you wait, if you knew a similar but standardized way of writing code in XAML will be available with the release? By the way, welcome to my web log.
This download is now outdated. Solution files are updated on the following parts of this series. I know you want to get your hands dirty, but please keep reading. Thanks to chaiguy1337 for reminding.

5 thoughts on “WPFix Part 1 (Lambda Converter Extension)”

  1. Thanks for your work. It is really amazing.

    At first i couldn’t build your solution because c# was complaining about the class MultiBindingExtension, so i had to comment out that line and it is compiled.

    I have another question about using string literals in lambda expressions. In c# i can use lambda expressions like

    dt => “Time is “+ dt.ToShortDateTime()

    but I couldn’t figure out how to do it in XAML. I thought i could type ” instead of ” character but it didn’t work.

    Can you suggest a solution for that?


  2. The problem is a little bit parser related. There are 2 ways of setting values of attributes. You can either use quotes (") or apostrophes('). While using binding or another markup extension, we enclose the extension part ({…}) with one of these and the other one with serve as our literal quoter.
    In our case, we use this ability to specify the lambda expression as a string literal parameter to our LambdaExtension’s constructor. One would expect a similar opportunity to use the alternating character to be used inside the second literal. I am not an expert on this, but this may be an xml issue.
    Solution may change, because the problem can also change. Here are two possible solutions for two different scenarios:

    <TextBlock Text='{Binding Source={x:Static s:DateTime.Now}, Converter={fix:Lambda "dt=>"DateTime.Now > " + dt.ToShortTimeString()"}}'/>

    Notice that we set text attribute by ' and use ” for the expression. Inside the expression we can use character to escape for ” but not for '.

    <TextBlock Text="{Binding Source={x:Static s:DateTime.Now}, Converter={fix:Lambda 'dt=>dt.ToShortTimeString().Replace(':','.')'}}"/>

    This time, we had to escape for ' in the expression. So we had to set the attribute with ". What if we need to use a string literal and a character inside the expression? There is always an ugly solution. You can use &quot; and &apos; inside the expression.

    Thank you for your comment. I hope, I was able to solve your problem without complicating the matter.

    P.S. Thanks for pointing out the compile problem. I was going to provide a MultiBindingExtension but I could not decide the syntax yet. Until then, one must delete that line and recompile the solution.

  3. This is very nice.

    Only problem is that the VS2008 designer (Cider) considers it full of errors and won’t display. It will compile and run just fine.

  4. Excellent work! (Also thanks for clearing up the point concerning MultiBindingExtension as that threw me off at first).

    Now all we need is intellisense and syntax highlighting in XAML lambda expressions… or “xamdas”! ;)

Comments are closed.