DataContext Detection changed in Silverlight

I have a frequent question:

(1) Is there a good reason why Silverlight does not provide a DataContextChanged event? It seems like a lot of trouble can be avoided if someone from Microsoft just changed internal to public in the FrameworkElement class (like WPF).

(2) I found one or two different ways to hack your path into a DataContextChanged event using DependencyProperties in a particular configuration. But I can’t get them to work reliably. My testing so far seems to show that they fire a hacked DataContextChanged event just fine for the first class to which I connect them, but don't fire for any other classes. Anyone else run into this issue? Or is it even better that they work around him?

(3) The reason I let myself know when my DataContext has changed is because there are some UI operations that are hard to get right in XAML but trivial in coding; and for many of these things I need to handle the events raised by my ViewModel; and therefore I need to know when my ViewModel changed, so I can hook up event handlers. Is this an accurate view of the world? Or is it a fact that I want to deal with similar things in code - for a pretty good sign that my thoughts have gone off the rails? I'm not an MVVM purist: I just want to get good code from here quickly, and I don't care how I get there. Code-behind has recommended me well for over ten years, and I don’t want to completely abandon it. But does my pragmatism complicate me at this moment?

+5
source share
1 answer

"But my pragmatism complicates itself at this moment?"

I would not call it pragmatism. I would call it a fear of change; staying in your comfort zone. Life is actually much simpler if you abandon your old way of thinking and adopt a new one (and I know exactly what you mean - I was in the same boat as you with the code).

Now, from my soap box and to a more practical answer:

If you want to detect changes in your model, pin events that will allow you to detect changes in your model. The DataContext is not really a model ... All of your model objects will have an INotifyPropertyChanged implementation. You must either connect to this for this model, or connect to something similar for the ObservableCollection.

Silverlight / WPF makes all of these things easier than data binding now works.

Do not fight with the frame. Do not bring the old ASP.Net methods with you to this game ... This will not help you. This way you lose the best parts of the frame.

Greetings.

+2
source

Source: https://habr.com/ru/post/902654/


All Articles