Wednesday, March 4, 2009

Further Thoughts on WPF and XAML

I started to play with WPF and XAML a couple weeks back, and wrote a post about my first thoughts. Since then, I've been working through my first full-fledged WPF app, and I've figured out a ton more that I didn't understand at the time: both about the true strengths of WPF, and the things I still find really annoying. So some more thoughts:

  • The ability to easily modify templated controls is amazing. You can do things in minutes with a ListBox in XAML that would have taken months of tedious coding in WinForms.
  • The WPF designer in Visual Studio sucks. It gives you virtually no help in troubleshooting errors, it's slow, and among many other complaints, it is of no help whatsoever when you're trying to layout a DataTemplate. I've been reduced to numerous long, painful cycles of, "change/compile/run/look/swear/change again".
  • The WPF data-binding model is very powerful, but it's very subtle, difficult to get right, and very easy to get wrong. If anyone at Microsoft actually believes the marketing-speak that it's "simple" in addition to powerful, they need to have their head examined. For instance, I guess it's kinda cool that you can set bind properties on one control to another control, and can trigger behaviors when those properties change: but I can do this in a very straightforward way in C#, and in contrast, the XAML syntax for doing it is really annoying. If you didn't know XAML pretty darned well, would you have any idea what the code below is doing? (Hint: it displays a particular control when its parent ListBoxItem is selected.)
  • <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}" Value="True">     <Setter Property="Visibility" Value="Visible" TargetName="pledgeDetailsDockPanel" /> DataTrigger>

  • XAML desperately needs some debugging aids. Since it's a declarative and not a procedural language, it doesn't really make sense to "step through" code, but something like FireBug for XAML instead of HTML would make a ton of difference when you're trying to figure out why a particular {Binding} isn't working the way you expect. (Which, in my limited experience, is fairly close to always.)
  • Contrary to the raves I've read elsewhere, I'm convinced that XAML is an ugly, clunky language. Granted, there are certain things that it allows you to express with great ease: instantiating a control, for instance, and setting its properties. But if you have to interact with outside objects or even other controls in any but the most basic manner, things get ugly very quickly. If you've ever worked with an ObjectDataSource, especially if you had to pass it dynamic method parameters, you know what I mean. Compared to XAML in these instances, C++ is a model of clarity, and perl is a model of readability.
  • Here's an example of what I mean on that last point. Let's say that I've got a simple WPF app that converts Fahrenheit to Celsius, by calling the method ToCelsius() on a Fahrenheit class.

    In C# code-behind, I can get this to work with two lines of code:

    private void txtFahrenheit_TextChanged(object sender, TextChangedEventArgs e) {     Fahrenheit temp = new Fahrenheit(txtFahrenheit.Text);     lblCelsius.Content = temp.ToCelsius(); }

    Here's how you get those two lines of code to work in XAML (with all of the purely formatting attributes and tags removed):

    <Grid.Resources>     <ObjectDataProvider ObjectType="{x:Type local:Fahrenheit}" x:Key="fahrenheit">         <ObjectDataProvider.ConstructorParameters>             <system:String>system:String>         ObjectDataProvider.ConstructorParameters>     ObjectDataProvider>     <ObjectDataProvider ObjectInstance="{StaticResource fahrenheit}" MethodName="ToCelsius" x:Key="toCelsius" /> Grid.Resources> <TextBox>     <TextBox.Text>         <Binding Source="{StaticResource fahrenheit}">             <Binding.Path>ConstructorParameters[0]Binding.Path>             <Binding.BindsDirectlyToSource>TrueBinding.BindsDirectlyToSource>             <Binding.UpdateSourceTrigger>PropertyChangedBinding.UpdateSourceTrigger>         Binding>     TextBox.Text> TextBox> <Label>     <Label.Content>         <Binding Source="{StaticResource toCelsius}" />     Label.Content> Label>

    Those two lines of very simple, very comprehensible code have expanded into something like 16 new lines of very dense, intricate, complicated XML. Oh, and have I mentioned that you can't debug those 16 lines of code? And if you look closely, you'll notice that there's a nice ugly hack in there, where I do a sort of reverse-binding, with the Fahrenheit textbox as the target of a binding to the first parameter in the Fahrenheit constructor. You have to do this because the ObjectDataProvider is inexplicably not a DependencyObject, so you can't bind any of its properties to the properties of a control: you have to do it the other way around, and bind a control's properties to the ObjectDataProvider.

    Sigh.

    XAML and WPF still needs a bit of work.

    43 comments:

    Anonymous said...

    п»їhttp://paydayloansmaster.co.uk/#915720 starting payday loan business [url=http://paydayloansmaster.co.uk/#427789]payday loans UK[/url] payday loan vendor

    Anonymous said...

    http://genericviagraelite.net/#726026 buy viagra in singapore all customers

    Anonymous said...

    http://sildenafilrxpills.net/#703230 sildenafil online

    Anonymous said...

    http://viagraespanafarmacia.net/#943826 farmacocinetica sildenafil 50 mg [url=http://viagraespanafarmacia.net/#301619]comprar viagra online[/url] vorst 27 sildenafil

    Anonymous said...

    http://online-payday-loan.pro/#139615 payday loans in gainesville fl

    Anonymous said...

    viagra för kvinnor Viagra http://viagrasv.net/#623994 viagra olagligt [url=http://viagrasv.net/#870102]köpa viagra[/url] viagrasv.net

    Anonymous said...

    http://cialisesp.net/#843515 cialis precio mexico farmacia

    Anonymous said...

    http://viagra-cialis-de.net/#889877 viagra kaufen wo

    Anonymous said...

    Cialis nebivolol viagra http://viagra-cialis-de.net/#368078 viagra nachteile [url=http://viagra-cialis-de.net/#384351]viagra-cialis-de.net[/url] viagra für junge männer

    Anonymous said...

    391111 http://grandpaydayloan.co.uk/#688786 northstar payday loans

    Anonymous said...

    1377 http://grandpaydayloan.co.uk/#870350 payday loans 2500 payday loan payday advance in mississauga

    Anonymous said...

    п»ї936017 http://abc-payday-loan.net/#204166 payday loans lombard il [url=http://abc-payday-loan.net/#6500]payday loans[/url] how to pay off payday loans

    Anonymous said...

    518903 http://cialiscanadapharm.net/#460088 cialis buy online overnight shipping Canadian Pharmacy cheap cialis india

    Anonymous said...

    tadalafili.net http://tadalafili.net/#612856 buy tadalafil pills [url=http://tadalafili.net/#548757]tadalafili.net[/url] cheap cialis for sale

    Anonymous said...

    acheter viagra prix viagra pharmacie dysfonction Г©rectile http://viagraphfrance.net/#194377 durГ©e prix viagra [url=http://viagraphfrance.net/#955921]viagraphfrance.net[/url] cialis effet indesirable prix viagra

    Anonymous said...

    customer service order viagra http://viagragrandpharmacy.net/#116023 buy viagra in europe impotence treatment

    Anonymous said...

    sildenafil farmacologia conocido http://viagrafespana.net/#948228 sildenafil batrachotoxin

    Anonymous said...

    311825 http://nolvadexrxpharm.net/#354689 nolvadex buy for gyno nolvadex pharm nolvadex in canada tamoxifen

    Anonymous said...

    167810 http://cashforchristmas.pro/#816914 ohio new payday loan law

    Anonymous said...

    payday loans online military approved payday loans http://cashforchristmas.pro/#104598 payday loans in lubbock texas [url=http://cashforchristmas.pro/#252759]cashforchristmas.pro[/url] payday loans without checking account

    Anonymous said...

    284324 http://viagradeapotheke.net/#647619 ssri viagra

    Anonymous said...

    cialismedsonline.net buy cialis without a prescription http://cialismedsonline.net/#985283 buy female cialis [url=http://cialismedsonline.net/#643566]cialis pills[/url] where can i buy cialis without a prescription

    Anonymous said...

    761789 http://top-pay-day-loans.co.uk/#527659 payday loan careers [url=http://top-pay-day-loans.co.uk/#146115]pay day loans online[/url] nutrition facts payday

    Anonymous said...

    buy cialis online in usa http://cialistoppharm.net/#447914 buy soft cialis [url=http://cialistoppharm.net/#601390]Cialis[/url] buy cialis europe

    Anonymous said...

    payday loans santa clarita http://paydayloansukreview.co.uk/#946045 payday loans instant [url=http://paydayloansukreview.co.uk/#351800]payday review[/url] australia payday loan

    Anonymous said...

    instant payday loans project payday review blog http://www.gajoob.com/#624919 payday loan 100 [url=http://www.gajoob.com/#387824]www.gajoob.com[/url] payday loans on the weekend

    Anonymous said...

    tadalafil pills http://tadalafilshoponline.net/#946391 tadalafil shop [url=http://tadalafilshoponline.net/#655741]visit site[/url] cheap cialis professional

    Anonymous said...

    paydayloansukreview.co.uk payday loan tucson http://paydayloansukreview.co.uk/#267330 payday advance philadelphia [url=http://paydayloansukreview.co.uk/#440988]paydayloansukreview.co.uk[/url] no fax online payday loans

    Anonymous said...

    payday loans in lancaster ca http://www.gajoob.com/#637805 payday loans in metairie la

    Anonymous said...

    cialis levitra viagra http://comprarcialisespana.net/#332686 tadalafil de elter [url=http://comprarcialisespana.net/#837572]cialis espana[/url] venta de viagra en farmacias comprar cialis

    Anonymous said...

    782158 http://top-pay-day-loans.co.uk/#90619 payday loans edmonton ab

    Anonymous said...

    http://viagraprecioviagraefectossecundarios.net/#986944 sildenafil de 100mg óxido nítrico

    Anonymous said...

    ok sildenafil buy cheap viagra uk http://sildenafilok.net/#896852 sildenafil online [url=http://sildenafilok.net/#98623]sildenafil online[/url] low viagra prices

    Anonymous said...

    http://cialisgeneriqueenpharmacie.net/#953403 acheter tadalafil en ligne [url=http://cialisgeneriqueenpharmacie.net/#750867]cialis en France[/url] magasin generique cialis

    Anonymous said...

    fivepaydayloans.net payday loan irvine ca http://fivepaydayloans.net/#484730 payday loans littleton colorado [url=http://fivepaydayloans.net/#868820]online payday loans[/url] payday loan tucson arizona

    Anonymous said...

    351281 http://tadalafilall.net/#171902 tadalafil tablets side effects 259597 http://viagrakaufende.net/#58364 herbal viagra manufacturers п»ї526169 http://needpaydayloans.pro/#412715 payday loans abbotsford 395402 http://genericviagraall.net/#372792 lowest viagra prices cialis levitra 527935 http://clomidtopreview.net/#718670 how do you buy clomid

    Anonymous said...

    156066 http://genericviagraall.net/#585991 eli lilly viagra price 921192 http://tenpaydayloans.co.uk/#520292 money now payday loan 353945 http://tadalafilall.net/#38630 tadalafilall.net 528136 http://viagrakaufende.net/#763849 billig viagra

    Anonymous said...

    п»ї271680 http://needpaydayloans.pro/#543786 no employment verification payday loan [url=http://needpaydayloans.pro/#900497]Homepage[/url] northstar payday loans

    Anonymous said...

    http://viagrauksalesonline.co.uk#viagra-uk viagra price australia online - uk prices buy viagra

    Anonymous said...

    [url=http://bclomidon.net#jhxj]Clomid rating[/url] $cd4408! [url=http://onfastloans.net#oofq]onfastloans.net[/url] ~it4736. [url=http://bcialison.net#cialis-on]cialis online[/url] %mm6541@

    Anonymous said...

    Plain living and high thinking.
    http://www.buybeatsbydrdrexa.com/
    http://www.casquemonsterbeatser.com/
    http://www.ghdnewzealandshopa.com/
    http://www.michaelkorsoutletez.com/
    http://www.nflnikejerseysshopxs.com/
    http://www.burberryoutletusaxs.com/
    http://www.coachfactoryoutletsez.com/
    http://www.bottesuggpascheri.com/
    http://www.cheapfashionshoesas.com/

    Anonymous said...

    Mainly it will depend on the financial need and also the creditability of the borrower
    payday loans uk these loans are approved ab muscles 24 hour by lenders.

    Anonymous said...

    Hi colleagues, how is the whole thing, and what you would
    like to say about this post, in my view its in fact remarkable designed for me.


    my page sian