The only time I start throwing code into code files is when I cannot put it in the ViewModel or deeper in the objects graph.
For example, this is your first time, as mentioned above, C. Lawrence Wenham , fully solvable in XAML code. To achieve this effect, there is no need to resort to code back. And this example can be extended to interact with a collection that is not necessarily represented in a control such as listbox. You can write XAML code that responds to changes in the current item in the collection in the ViewModel through a binding.
Your second situation can also be achieved thanks to the dvalidation tools in the ViewModel and data binding to these objects with your XAML. IDataErrorInfo is a great mechanism that is built in precisely for this purpose. Here is a short article demonstrating the simple use of IDataErrorInfo.
Examples of when you need to look into the code are apparently small and far apart. One example that I came across is when a control does not support ICommand, and you cannot attach functionality to a behavioral element, an example of which is a ListBox. But there are some ways around this limitation, as shown in this wonderful SO question . In addition, in case you need to override the rendering in user controls, you need to do this in code or in an inherited class.
Hope this adds a bit more useful information to the answers.
Dave white
source share