MikeHouston.net

Honorary member of the Shaolin

Binding in XAML sux!

It appears there is no ‘real’ implementation for formatting or any ‘expressions’ in the binding syntax for XAML.  What a POS!

In order to format dates when binding in XAML I had to…

Create a implementation of IValueConverter  :

    public class ShortDateFormatConverter : IValueConverter

    {

        /*point of interest here – targetType appears to always be string; no matter what the actual type bound to is---WTF?*/

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

        {

            DateTime dateToFormat;           

            if (DateTime.TryParse(value.ToString(), out dateToFormat))

            {

                /*this is just so I could pass in any of the ‘format’ methods*/

                return dateToFormat.GetType().InvokeMember(parameter.ToString(), System.Reflection.BindingFlags.InvokeMethod, null, dateToFormat,null).ToString();

            }

            else

            {

                return string.Empty;

            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

        {

            throw new NotSupportedException("Cannot convert back at this time - if you need this access the object's individual properties");

        }

    }

 

Import the namespace :

<Window x:Class="test.Window1"

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

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

    xmlns:cvrt="clr-namespace:test"

    Title="testing" Height="300" Width="709">

 

Set up the Converter to be used in the markup (create window resource) :

  <Window.Resources>

    <cvrt:StringConverter x:Key="StringConverter" />

    <cvrt:ShortDateFormatConverter x:Key="ShortDateFormatConverter" />

  </Window.Resources

 

At this point, I’m finally finished putting all the crap in place to actually bind a property to a ui object and use the converter to format it :

    <TextBox Text="{Binding Path=DtTest, Converter={StaticResource ShortDateFormatConverter}, ConverterParameter=ToLongDateString}" />

<!—any of the ‘format’ methods on DateTime will work here, like ToShortDateString, etc. à

 

If there really isn’t a ‘built-in’ way to apply basic expressions when binding in the markup then DataBinding in XAML is completely useless…and a half-ass implementation at best! 

That’s my last rant on that for the day ;-P

Technorati Tags: , , ,

Comments (36) -

  • Rohs Screening

    7/2/2009 7:22:11 PM |

    Hi,
      The X-ray light produced by the atoms is very weak. In an effort to make the X-rays more powerful, scientists have attempted using higher-powered lasers on the electrons, but success has been limited.

  • search engine optimisation company

    8/15/2009 9:01:34 PM |

    The SEO Slugs plugin generates SEO friendly “post slugs” from your post’s title. This optimizes the ability of search engine to index your content appropriately thus providing you with the optimum ability to attract visitors to your site.

  • pet strollers

    9/11/2009 7:51:02 PM |

    I posted your blog to my facebook group


    Regards

    Kissy

  • cash loans

    10/26/2009 9:15:59 PM |

    Like your writing! Still you can do some things to improve it.

  • cash advance

    11/8/2009 11:15:37 PM |

    Do you have any more info on this?

  • payday loans

    11/17/2009 3:40:43 AM |

    This is very detailed and informative article. Thanks for helping me solve my dilemma!

  • freelance seo

    11/21/2009 5:32:42 AM |

    I’m intrigued by how popular All-In-One is – I’m a much bigger fan of Platinum SEO.

  • proxy sites

    11/23/2009 3:31:13 AM |

    Keep up the good work bro.Your article is really great and I truly enjoyed reading it.Waiting for some more great articles like this from you in the coming days.

  • payday loans

    11/24/2009 10:59:53 PM |

    thanks!  very helpful post!! like the template btw ;)

  • payday loans

    11/25/2009 1:35:25 AM |

    Do you make money out of this blog? just curious

  • payday loans

    11/25/2009 1:44:45 AM |

    Nice resource. rss feed added

  • cash loans

    11/28/2009 1:49:38 AM |

    Like your writing! Still you can do some things to improve it.

  • oes tsetnoc faq

    11/30/2009 3:06:08 AM |

    Well, I just found your blog through search engine. Actually I didn't intend to visit it before, yet after I read your article, I just can say that it's so inspiring. Thanks for making such nice article!

  • links of london

    11/30/2009 5:32:23 PM |

    your article is excellent,i really love it. hoping to read your following post

  • Acai Berry

    11/30/2009 8:35:11 PM |

    Excellent post.This was actually what I was looking for, and I am glad that I finally came here! Thanks for sharing the such information with us.

  • search engine optimisation

    12/1/2009 12:38:22 AM |

    Hey that was cool, Can I take part of your post to my blog, i will post the original source of course.

  • payday loans

    12/5/2009 4:55:23 AM |

    Nice resource. rss feed added

  • fast personal loans

    12/8/2009 9:36:57 AM |

    Hey very nice blog!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also...

  • faxless payday loans

    12/9/2009 11:30:46 AM |

    This is exactly what i was looking for. thank you for the informative post and keep up the good work!

  • flash programming

    12/10/2009 9:57:57 PM |

    I could never think that surfing would be so useful for me when I found you blog.  

  • Colon Cleanse

    12/16/2009 4:07:34 PM |

    You have done good job as these types of things are very useful and easily understandable.

  • Colon Cleanse

    12/16/2009 4:38:21 PM |

    This code given here is fully operational. It made my work easier.

  • Pork Sandwich Recipes

    12/18/2009 9:04:54 AM |

    Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

  • us online casinos jes

    12/20/2009 6:41:51 PM |

    When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four emails with the same comment.
    Is there any way you can remove me from that service?
    Thanks!

  • us online casinos jes

    12/20/2009 6:43:51 PM |

    You got a really useful blog I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me.

  • us online casinos art

    12/20/2009 7:59:13 PM |

    Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.

  • usa online casinos jes

    12/20/2009 9:19:31 PM |

    I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.
    Great stuff as usual...

  • usa online casinos

    12/21/2009 5:47:20 AM |

    When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four emails with the same comment.
    Is there any way you can remove me from that service?
    Thanks!

  • internet marketing consultant

    12/22/2009 8:18:33 AM |

    Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.

  • make money taking surveys

    12/22/2009 9:15:48 AM |

    I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

  • Balance Scale

    12/22/2009 10:02:09 AM |

    I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

Comments are closed