【发布时间】:2015-02-20 22:08:49
【问题描述】:
所以在 Caliburn Micro 中,我一直在使用以下方法在另一个视图中组合视图:
- 在组合视图中放置一个 ContentControl。
- 在组合 ViewModel 上创建一个属性,并将组合 ViewModel 分配给它
- 为 ContentControl 提供一个 x:Name 属性,该属性与组合 ViewModel 上组合 ViewModel 属性的名称相匹配。
像这样……
查看:
<UserControl x:Class="MyProject.MyComposingView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<ContentControl x:Name="MyComposedViewModel"/>
</UserControl>
视图模型:
class ComposingViewModel : PropertyChangedBase
{
private ComposedViewModel _myComposedViewModel;
public ComposedViewModel MyComposedViewModel
{
get { return _myComposedViewModel; }
set
{
_myComposedViewModel= value;
NotifyOfPropertyChange(() => Page);
}
}
public ComposingViewModel(ComposedViewModel myComposedViewModel)
{
MyComposedViewModel = myComposedViewModel;
}
}
Caliburn Micro 自动发现,因为它是一个 ContentControl,它显然不想绑定到 ViewModel,而是绑定到其关联的 View,因此它在后台做了一些事情来将 ContentControl 的 Content 属性绑定到 MyComposedView 而不是我的组合视图模型。
但是,如果我不想使用 ContentControl 怎么办?就像,也许我的一些可重用的自定义组件包装了 ContentControl?例如:
<UserControl x:Class="MyProject.MyContentWrapper"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
<Grid x:Name="PreviewBox" SizeChanged="onSizeChanged">
<Image x:Name="BGImage" Source="{Binding BGImage}"/>
<ContentControl Content="{Binding}"/>
</Grid>
</UserControl>
如果我用 MyContentWrapper 替换 ContentControl,CaliburnMicro 将不再发挥它的魔力来提供 MyComposedView,我最终会得到一个 TextBlock,上面写着 MyProject.MyComposedViewModel。
如何让 CaliburnMicro 知道它应该提供 View 而不是 ViewModel?
【问题讨论】:
标签: c# wpf caliburn.micro