WPF MVVM Code Behind

后端 未结 5 1469
悲哀的现实
悲哀的现实 2020-12-10 02:08

I try to avoid code behind in views, within my WPF MVVM project.

However I have some things that are very specific to the view. For example when a control gets focus

5条回答
  •  半阙折子戏
    2020-12-10 03:04

    So I'm asking if the code only affects the view (e.g. things like cursor movement, selecting all text in a text box etc..., and not the model or view model, is it okay to put it in code behind, rather than elsewhere.

    Not only it is OK, but it is strongly encouraged. MVVM is not here for you to write thousands of ugly lines of code in ViewModels, it's here to make the code testable and to introduce a separation of concerns.

    If it's purely related to the view (your "focus" example is a perfect example), then just write it in the code behind.

提交回复
热议问题