我应该在哪里设置 DataContext - 代码隐藏或 xaml?
(老实说,我搜索并阅读了所有似乎相关的“相关问题” - 我确实希望我没有“错过”其他地方的这个问题,但这里是......)
(至少)有两种不同的方法来设置 DataContext 。可以使用 XAML,也可以使用隐藏代码。
什么是“最佳实践”?为什么?
我倾向于在 XAML 中设置它,因为它允许设计者自己定义集合,但我需要“弹药”来解释为什么它是最佳实践或者为什么我很疯狂,而背后的代码是炸弹......
(honestly I searched and read all the 'related questions' that seemed relevant - i do hope i didn't "miss" this question from elsewhere but here goes...)
There are two different ways (at least) to set the DataContext. One can use XAML or one can use the code behind.
What is the 'best practice' and why?
I tend to favor setting it in XAML because it allows a designer to define collections on their own but I need 'ammunition' on why it's a best practice or why I'm crazy and the code behind is the bomb...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能会考虑的第三种方法是使用定位器服务。我通常有一个类负责创建我的所有 DataContext(大多数情况下是 VM),并且我在 App.xaml 资源中创建该类的一个实例。然后,我在每个单独页面的 XAML 中绑定 DataContext。
IE
A third way you might look at is using a locator service. I usually have one class that is responsible for the creation of all my DataContext(VM's in most cases for me) and I create an instance of that class in the App.xaml Resources. Then I bind the DataContext in the XAML of each individual page.
i.e.
我认为这取决于您将 DataContext 设置为什么,以及最终的个人喜好。
我个人总是在我的视图后面的代码中这样做,因为我发现它整体上更干净,这就是我学习 MVVM 的方式。另一件需要记住的事情是,有时您可能需要根据您正在使用的内容更改数据上下文。如果是这种情况,那么在后面的代码中比在 XAML 中执行要干净/容易得多。
I think it depends on what you are setting the DataContext to, and ultimately personal preference.
I personally always do it in the code behind of my views because I find it overall cleaner, and it was how I was taught MVVM. Another thing to keep in mind is, there are times you may need to change your datacontext depending on what you are working with. If this is the case it's much cleaner/easier to do in the code behind rather than in XAML.
从目前的答案可以看出,意见存在分歧。事实上,不存在最佳实践(我确实对 Silverlight 世界中“最佳实践”的讨论感到恼火,对于最佳实践来说,它还太年轻,无法真正为人所知。)
事实上,您无法设置Xaml 中的“数据上下文”。除非您实际上构造了这样的对象实例: -
最终外部的东西必须直接或通过另一个属性或通过绑定间接分配 DataContext 属性(如 Stephan 的答案)。正是这个外部上下文决定了在 Xaml 中执行此操作是否有意义。许多 MVVM 解决方案使用 Xaml 中的绑定,在某些情况下只是为了避免代码隐藏中必须有任何代码,而不是真正“更好”。其他人使用控件派生自的基类在代码中设置 DataContext。
As you can see by the answers so far opinion is divided. In truth there is no best practice (I do get bee in my bonet about discusions of "best practice" in the Silverlight world, its way too young for best practice to be truely known.)
The reality actually is that you can't set the "data context" in Xaml. Unless you actually construct an object instance like this:-
Ultimately something external has to assign either the DataContext property directly or indirectly via another property or via binding (as in Stephan's answer). Its this external context which is dictates whether it makes sense to do it in Xaml or not. Many MVVM solutions use a binding in Xaml, in some cases simply to avoid there having to be any code at all in code-behind rather than it truely being "better". Others set up the DataContext in code using a base class that your control derives from.
我假设的用户控件/视图的 DataContext?在后台代码中设置数据上下文的优点之一是依赖项注入的可用性。您的 DI 容器可以在运行时动态地为您处理任何依赖项。
通过这种模式,我经常使用 d:DataContext 在 xaml 中设置视图的 Blend design DataContext。 “设计版本”可以提供在 Blend 中使用的模拟数据,而真正的实现则在运行时解决。
DataContext of the user control/view I assume? One advantage of setting data context in the code behind is the availability of dependency injection. Your DI container can take care of any dependencies for you dynamically at run-time.
With this pattern, I frequently set a view's Blend design DataContext in xaml using d:DataContext. The "design version" can provide mock data for use in Blend, while the true implementation is resolved at run-time.