使用 ListBox(或其他 ItemsControl)托管 Caliburn 演示者
如果我有一个 MultiPresenter
并且我使用 ListBox
来显示它托管的 Presenters
,我如何让 Caliburn 发现并绑定项目的视图和视图模型?
例如,如果我有一个看起来像这样的简单视图:
<UserControl x:Class="MyProject.Views.CarView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ListBox ItemsSource="{Binding Parts}" />
</Grid>
</UserControl>
它绑定到 CarViewModel
:
public class CarViewModel : MultiPresenter
{
public BindableCollection<IPartViewModel> Parts { get; }
}
并且 Parts
集合包含实现 IPresenter< 的各种对象/code> 并具有相应的视图,例如
WheelViewModel
和 WheelView
,以及 EngineViewModel
和 EngineView
。
我希望 Caliburn 使用视图策略为我解决视图。这可能吗?在这种情况下,我需要做什么才能正确设置演示者的层次结构?
If I have a MultiPresenter
and I am using a ListBox
to display the Presenters
it is hosting, how do I get Caliburn to discover and bind the views and view models for the items?
For example, if I have a simple view that looks something like this:
<UserControl x:Class="MyProject.Views.CarView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ListBox ItemsSource="{Binding Parts}" />
</Grid>
</UserControl>
Which is bound to the CarViewModel
:
public class CarViewModel : MultiPresenter
{
public BindableCollection<IPartViewModel> Parts { get; }
}
And the Parts
collection contains various objects that implement IPresenter
and have corresponding views, e.g. WheelViewModel
and WheelView
, and EngineViewModel
and EngineView
.
I'd like Caliburn to resolve the views for me using the view strategy. Is this possible? What do I need to do to correctly set up the hierarchy of presenters in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必为此更改演示者层次结构。我只建议您考虑使用
MultiPresenter.Presenters
属性来收集子 ViewModel 以及MultiPresenter.Open
和MultiPresenter.Shutdown
方法(如果需要)强制子 ViewModel 生命周期。对于绑定问题,您应该为 ListBox 项定义模板:
使用
cal:View.Model
附加属性,框架负责为每个 ViewModel 创建适当的 View,将其绑定到 ViewModel 并将其注入到 ContentControl 中。您还应该确保 View 和 ViewModel 的命名空间和类命名遵循 Caliburn 默认约定 如果您希望框架正确推断您的视图。否则,您必须编写一个自定义 IViewStrategy(但这并不难)。
编辑:修复了 cal:View.Model 属性中的绑定表达式
You don't have to change presenter hierarchy for this. I only suggest you to consider using the
MultiPresenter.Presenters
property to collect child ViewModels and theMultiPresenter.Open
andMultiPresenter.Shutdown
methods if you need to enforce child ViewModels lifecycle.For the binding issue, you should define the template for the ListBox items:
Using
cal:View.Model
attached property, the framework takes care of creating an appropriate View for each ViewModel, binding it to the ViewModel and injecting it into the ContentControl.You should also make sure that your namespace and class naming for Views and ViewModels follows the Caliburn default convention if you want your Views to be correctly inferred by the framework. Otherwise, you have to write a custom IViewStrategy (it's not hard, though).
Edit: fixed binding expression in cal:View.Model property