locked
Events in XAML RRS feed

  • Question

  • Hi all,

    I have been doing WinForms since forever (well, ok ... 2002) and I consider myself quite good at it. But now, it's time for me to delve into WPF.

    My question concerns the fact that the designer seems to want to put the event wiring in the XAML and I *strongly* disagree with this! My opinion is that the XAML should be for design stuff only and events are decidely NOT design stuff.

    Yeah, I *can* wire up the event in the code myself and it works fine. But I can see no way to have the IDE put it in the code if I enter it in the Property sheet ... I was looking for some setting or something to allow this, but I can't find anything. Anyone know if this is possible?

    TIA

     


    ~~Bonnie Berent [C# MVP]
    Thursday, September 3, 2009 12:47 AM

Answers

  • Thanks Reed. It's funny, but about 10 minutes after I posted my question, the book I was reading started on the subject of ICommand. And I have also run across MVVM as it applies to WPF (I have a few articles bookmarked, in addition to the links you provided in your post).

    So, I obviously should get into reading more about that stuff before I ask any more silly questions. ;0) Thanks for taking the time to answer.
    ~~Bonnie Berent [C# MVP]

    It's definitely not a silly question ;)

    WPF is really hard to get used to if you're used to thinking in terms of Windows Forms.  Attaching code directly to an event handler is almost never done (in scalable WPF patterns), and with Windows Forms, it was the only way to work.  There's definitely a tough transitional process in terms of thinking differently, but once you get used to it, it's much, much nicer to work with and program against.



    Reed Copsey, Jr. - http://reedcopsey.com
    • Proposed as answer by Zhi-Xin Ye Friday, September 4, 2009 5:41 AM
    • Marked as answer by Zhi-Xin Ye Monday, September 7, 2009 11:30 AM
    Thursday, September 3, 2009 4:48 PM

All replies

  • When you start working with WPF, it's really better to start thinking in terms WPF "likes" instead of the way of working that Windows Forms "liked".

    The reason events aren't wired up in code, the way you are expecting with Windows Forms, is due to the flexibility provided by other means in WPF.  Instead of wiring up events in code, it's usually a much better option to work with ICommand -derived commands.  This lets you completely separate the code in the event from the object that "fires" it, and works much more cleanly for large projects than the event-tied-to-control model that Windows forms used.

    I recommend reading up on the Model-View-ViewModel pattern .  It takes implementing this once to understand why this provides a nicer experience than Windows Forms, but once you get it, you'll never want to go back.


    Reed Copsey, Jr. - http://reedcopsey.com
    Thursday, September 3, 2009 1:11 AM
  • Thanks Reed. It's funny, but about 10 minutes after I posted my question, the book I was reading started on the subject of ICommand. And I have also run across MVVM as it applies to WPF (I have a few articles bookmarked, in addition to the links you provided in your post).

    So, I obviously should get into reading more about that stuff before I ask any more silly questions. ;0) Thanks for taking the time to answer.
    ~~Bonnie Berent [C# MVP]
    Thursday, September 3, 2009 2:06 PM
  • Thanks Reed. It's funny, but about 10 minutes after I posted my question, the book I was reading started on the subject of ICommand. And I have also run across MVVM as it applies to WPF (I have a few articles bookmarked, in addition to the links you provided in your post).

    So, I obviously should get into reading more about that stuff before I ask any more silly questions. ;0) Thanks for taking the time to answer.
    ~~Bonnie Berent [C# MVP]

    It's definitely not a silly question ;)

    WPF is really hard to get used to if you're used to thinking in terms of Windows Forms.  Attaching code directly to an event handler is almost never done (in scalable WPF patterns), and with Windows Forms, it was the only way to work.  There's definitely a tough transitional process in terms of thinking differently, but once you get used to it, it's much, much nicer to work with and program against.



    Reed Copsey, Jr. - http://reedcopsey.com
    • Proposed as answer by Zhi-Xin Ye Friday, September 4, 2009 5:41 AM
    • Marked as answer by Zhi-Xin Ye Monday, September 7, 2009 11:30 AM
    Thursday, September 3, 2009 4:48 PM
  • Thanks for the words of encouragement Reed. I hope to get my head wrapped around it soon.
    ~~Bonnie Berent [C# MVP]
    Thursday, September 3, 2009 11:16 PM