检测 Silverlight 中更改的 DataContext [英] Detecting DataContext changed in Silverlight

查看:26
本文介绍了检测 Silverlight 中更改的 DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多部分的问题:

I have a multi-part question:

(1) Silverlight 不公开 DataContextChanged 事件是否有充分的理由?如果 Microsoft 的某人只是将 FrameworkElement 类中的 internal 更改为 public(就像 WPF 那样),似乎可以避免很多麻烦.

(1) Is there a good reason why Silverlight doesn't expose a DataContextChanged event? It seems like a whole lot of hassle could be avoided if someone at Microsoft just changed internal to public in the FrameworkElement class (like WPF does).

(2) 我找到了一个两种 不同的方法,用于通过使用入侵 DataContextChanged 事件一种或另一种配置中的 DependencyProperties.但我无法让它们可靠地工作.到目前为止,我的测试似乎表明,它们为我将它们连接到的第一个类触发了被黑的 DataContextChanged 事件,但不会为任何其他类触发.有没有其他人遇到过这个问题?或者更好的是,他们是否已经解决了这个问题?

(2) I've found one or two different methods for hacking your way into a DataContextChanged event by using DependencyProperties in one configuration or another. But I can't get them to work reliably. My testing so far seems to show that they fire the hacked DataContextChanged event just fine for the first class to which I hook them up, but don't fire for any other classes. Has anybody else run into that problem? Or better yet, have they worked their way around it?

(3) 我一直想知道我的 DataContext 何时发生变化的原因是,有一些 UI 操作在 XAML 中很难正确处理,但在代码隐藏中却很简单;对于其中的许多事情,我需要处理由我的 ViewModel 引发的事件;因此我需要知道我的 ViewModel 何时发生变化,以便我可以连接事件处理程序.这是正确的世界观吗?或者我想在代码隐藏中处理这类事情的事实很好地表明我的想法已经偏离了轨道?我不是 MVVM 纯粹主义者:我只是想从这里快速获得好的代码,我并不特别关心我是如何到达那里的.十多年来,代码隐藏已经为我提供了相当好的服务,我不愿意完全放弃它.但我的实用主义是否让我在这一点上变得更难了?

(3) The reason I've been giving myself for wanting to know when my DataContext has changed is that there are some UI operations that are complicated to get right in XAML, but are trivial in code-behind; and for many of those things, I need to handle events raised by my ViewModel; and hence I need to know when my ViewModel has changed, so I can wire up the event handlers. Is this an accurate view of the world? Or is the fact that I'm wanting to deal with this sort of thing in code-behind a pretty good indication that my thinking has gone off the rails some ways back? I'm no MVVM purist: I just want to get from here to good code quickly, and I don't particularly care how I get there. Code-behind has served me reasonably well for over a decade now, and I'm loth to abandon it entirely. But is my pragmatism making it harder on myself at this point?

推荐答案

但是我的实用主义是否让它变得更难了在这一点上我自己?"

"But is my pragmatism making it harder on myself at this point?"

我不会称之为实用主义.我称之为害怕改变;留在你的舒适区.如果你放弃旧的思维方式并接受新的思维方式,生活实际上会容易得多(我确切地知道你的意思 - 我和你在代码隐藏方面处于同一条船上).

I wouldn't call it pragmatism. I'd call it fear of change; staying in your comfort zone. Life is actually far easier if you abandon your old way of thinking and embrace the new (and I know exactly what you mean - I was in the same boat as you with code-behind).

现在,离开我的肥皂盒,得到一个更实用的答案:

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

当您想要检测模型中的更改时,请连接到允许您检测模型中的更改的事件.DataContext 并不是真正的模型...您的所有模型对象都将具有 INotifyPropertyChanged 的​​实现.您应该为给定的模型挂钩,或者为 ObservableCollection 挂钩类似的东西.

When you want to detect changes in your model, then hook into the events that allow you to detect changes in your model. The DataContext is not really the model... All of your model objects are going to have an implementation of INotifyPropertyChanged. You should either be hooking into that for a given model, or hooking into something similar for an ObservableCollection.

Silverlight/WPF 使所有这类事情变得更容易,因为数据绑定实际上有效.

Silverlight/WPF make all of this sort of stuff WAY easier now that databinding actually works.

不要对抗框架.不要将旧的 ASP.Net 实践带到这个游戏中......它对你没有帮助.这样你就会失去框架最好的部分.

Don't fight the framework. Don't bring old ASP.Net practices with you to this game... It will not help you. You'll lose the best parts of the framework's power that way.

干杯.

这篇关于检测 Silverlight 中更改的 DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆