next best geek
this blog cannot be explained in a few words

WPF: Code in XAML -> Width={Binding Height}*2?

April 10th, 2008 . by Tobias

This posting consists of two sections:

  1. cool but not really useable
  2. really cool and usable

lets start with the non-usable stuff [Code in XAML]:

Whenever you declare your UI in XAML you've to attach event handlers in the code behind file, right? I expected that there has to be another way to attach code as XAML is just XML which is used to generate .cs (g.cs) files. And, tada, there is one:

    1 <Window x:Class="App1.Window1"

    2   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    3   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    4   Title="Window1" Height="300" Width="300">

    5     <Grid>

    6 

    7         <x:Code>

    8             <![CDATA[

    9               private void ClickMe(Object sender, RoutedEventArgs e)

   10               {

   11                   MessageBox.Show("This is embedded code from a CDATA section!");

   12               }

   13             ]]>

   14         </x:Code>

   15 

   16         <Button Content="Click me" Click="ClickMe" />

   17     </Grid>

   18 </Window>

let me cite Uri Geller: unbelievable ;) This will become a mess if there's more code involved. On the other hand: It should be possible to use this "approach" together with the XAML loader to implement a new type of plugin system: Your code gets part of the original code. Didn't test it yet and I hope it doesn't work as this might lead to new security holes.

Let's go on with the second, the really usefull, section [Width={Binding Height}*2]:

As there are a lot of guys out there who are a lot smarter than I, let me concentrate on pasting the following links without any error in this post :D:

Both present ways to define converters for WPF in XAML. Yeah, you got it right, no more one-line converters. Now you can create bindings which can not only bind to properties but can also calculate on those properties.
Once again: unbelievable.

I hope this post is as useful for you as the above links were useful for me.

Regards,

-t


One Response to “WPF: Code in XAML -> Width={Binding Height}*2?”

  1. comment number 1 by: Tina Russell

    I finally decided to write a comment on your blog. I just wanted to say good job. I really enjoy reading your posts.

    Tina Russell

Leave a Reply

You must be logged in to post a comment.