问题描述
我将ViewModels绑定到ContentControls,让Caliburn负责创建和绑定视图。但是,我想根据我要绑定到的ContentControl来自定义Caliburns ViewModel-> View约定。
I'm binding ViewModels to ContentControls and letting Caliburn taking care of creating and binding the view. However, I would like to customize Caliburns ViewModel->View convention based on which ContentControl I'm binding to.
例如,一个常规的ContentControl,我想要一个常规的View绑定(SomethingViewModel-> SomethingView)。但是对于某些ContentControl,我希望Caliburn获取一个名为SomethingViewSpecial的视图。
For example, a regular ContentControl, I want the regular View to bind (SomethingViewModel -> SomethingView). But for a certain ContentControl, I want Caliburn to fetch a View named for example SomethingViewSpecial. Is that possible?
推荐答案
是的,有可能。
使用 cal:View.Context
附加属性,然后为指定的上下文命名视图,例如 YourNamespace.Something.ContextView
(从视图模型名称中删除 ViewModel,添加一个点,以及Context属性的值)。
Use cal:View.Context
attached property and then name your view for the specified context like YourNamespace.Something.ContextView
(remove "ViewModel" from your view model name, add a dot, and the value of Context property).
那样,您甚至可以将多个视图绑定到一个视图模型。
By that, you can even bind several views to one view model.
示例:
<ContentControl x:Name="Toolbar" cal:View.Model="{Binding ActiveItem}" cal:View.Context="Toolbar" />
您可以看到工作示例(摘自示例应用程序,)。
You can see working example here (from CoProject sample application, part 9).
这篇关于取决于ContentControl(Caliburn.Micro)的不同视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!