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.

    56 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://genericviagraelite.net/#522432 biochemistry buy viagra Viagra Elite http://paydayloansabc.co.uk/#319212 payday loan lake forest pay day loans uk

    Anonymous said...

    http://viagraespanafarmacia.net/#900322 sildenafil y nero [url=http://viagraespanafarmacia.net/#765266]viagra generico[/url] sildenafil droga efectos secundarios http://tadalafilrxpills.net/#709132 tadalafil [url=http://tadalafilrxpills.net/#594792]order tadalafil[/url] cialis by mail order

    Anonymous said...

    pay day loans Abc payday america bloomington mn http://paydayloansabc.co.uk/#886857 direct payday loan sites [url=http://paydayloansabc.co.uk/#359959]pay day loans for bad credit[/url] last chance payday
    viagra naturel masculina viagra achat http://viagraprixfrance.net/#545811 viagra achat suisse [url=http://viagraprixfrance.net/#422418]prix viagra pharmacie[/url] prix viagra 100mg sildenafil citrate
    viagra precio en espaГ±a sildenafil regalo http://viagraespanafarmacia.net/#836105 citrato de sildenafil generico [url=http://viagraespanafarmacia.net/#699091]comprar viagra[/url] cooper sildenafil efectos secundarios

    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/#355866 purpose money payday loan online payday loan http://sildenafilrxpills.net/#132092 buy sildenafil order sildenafil

    Anonymous said...

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

    Anonymous said...

    viagra sin receta sildenafil para mujeres impotencia masculina http://viagraespanafarmacia.net/#124223 oferta del sildenafil [url=http://viagraespanafarmacia.net/#445171]comprar viagra[/url] sildenafil para adelgazar
    acheter viagra vente en ligne acheter viagra http://viagraprixfrance.net/#364242 viagra achat en pharmacie [url=http://viagraprixfrance.net/#229851]prix viagra pharmacie[/url] acheter viagra uprima

    Anonymous said...

    п»їhttp://genericviagraelite.net/#328639 buy viagra online properly [url=http://genericviagraelite.net/#713083]Generic Viagra[/url] buy viagra in uk online consultation http://paydayloansabc.co.uk/#403006 payday loans in austin texas [url=http://paydayloansabc.co.uk/#743360]pay day loans for bad credit[/url] payday advance garland tx http://tadalafilrxpills.net/#376004 tadalafil pharmacy [url=http://tadalafilrxpills.net/#473324]tadalafilrxpills.net[/url] buy cialis uk

    Anonymous said...

    payday loans uk payday loan md http://jackpaydayloans.co.uk/#298490 cash advance payday loans [url=http://jackpaydayloans.co.uk/#904769]jackpaydayloans.co.uk[/url] preferred payday
    п»їcheap cialis online cheap cialis canada http://cialisonlinecheapestcialis.net/#711172 buy cialis online canada [url=http://cialisonlinecheapestcialis.net/#323567]cheapest cialis online[/url] generic cialis buy
    cheap kamagra uk uk kamagra http://kamagraen.co.uk/#276368 tablets on line cheap kamagra [url=http://kamagraen.co.uk/#219550]kamagra fast[/url] cheap kamagra generic viagra

    Anonymous said...

    п»їCialis buy cialis online australia http://cialisonlinecheapestcialis.net/#36027 buying cialis online [url=http://cialisonlinecheapestcialis.net/#169745]cialisonlinecheapestcialis.net[/url] buy cialis online without a prescription
    cialis effet secondaire acheter sur internet viagra 100mg http://pharmaciealexfrance.net/#523219 viagra achat 100 [url=http://pharmaciealexfrance.net/#238757]acheter viagra[/url] reliable supplier viagra 100mg

    Anonymous said...

    Payday Loans Father payday loans in aurora http://fatherpaydayloans.ca/#998758 payday loans legit [url=http://fatherpaydayloans.ca/#378491]payday loans[/url] payday loans canadian
    pharmacie Alex acheter viagra cialis libre belgique http://pharmaciealexfrance.net/#202866 levitra le moins cher acheter viagra [url=http://pharmaciealexfrance.net/#821403]cialis prix[/url] parole maudit acheter viagra

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

    Cialis cialis farmacia lilly icos http://viagra-cialis-italia.net/#689111 rezept cialis generico [url=http://viagra-cialis-italia.net/#796688]cialis italia[/url] pramil cialis online cialisesp.net msds cialis generico http://cialisesp.net/#855757 cialis 5 mg posologia [url=http://cialisesp.net/#887698]cialisesp.net[/url] cialis generico foro

    Anonymous said...

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

    Anonymous said...

    112219 http://cashadvanceloans.pro/#370633 payday in advance cash advance payday loans sherman tx 597119 http://happyloansbc.co.uk/#768651 loans till payday uk payday loans for bad credit payday loans history

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

    457898 http://cialisesp.net/#203354 maxfil tadalafil comprar cialis tadalafil causas 873098 http://viagrasv.net/#515711 kopa viagra online viagra saljes viagrasv.net

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

    paydayloans-paydayloan.co.uk nate van dyke payday http://paydayloans-paydayloan.co.uk/#647924 payday loan reno nv [url=http://paydayloans-paydayloan.co.uk/#813983]payday loans uk[/url] payday loans broken arrow ok Viagra acheter viagra parapharmacie http://viagraprixviagraenligne.net/#950853 cialis en france acheter viagra [url=http://viagraprixviagraenligne.net/#744142]viagra en ligne[/url] pfizer vente viagra 100mg viagra sildenafil de roche http://viagraprecioviagraefectossecundarios.net/#961534 sildenafil vendo farmacia online [url=http://viagraprecioviagraefectossecundarios.net/#977838]viagraprecioviagraefectossecundarios.net[/url] uso de sildenafil cialiserfahrungencialisrezeptfrei.net cialiserfahrungencialisrezeptfrei.net http://cialiserfahrungencialisrezeptfrei.net/#190438 cialis generika kaufen [url=http://cialiserfahrungencialisrezeptfrei.net/#482680]cialiserfahrungencialisrezeptfrei.net[/url] cialis preisvergleich

    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