Wpf Caliburn Micro自替换视图/视图模型
我有一个列表框来选择要编辑的项目。我还有一个编辑按钮。将其称为MainView[模型] 如果我按下编辑按钮,MainView[模型]将被EditView[模型]替换。 编辑视图不得显示在主视图下方或旁边的区域。它应该被完全替换,或者至少完全隐藏主视图 如果编辑完成(确定,取消),应再次显示主视图 我试图覆盖ContentControl,但没有成功。 现在,我在考虑一种NavigatorViewModel,它有一个属性公开的多个ViewModel。但我不确定这是不是正确的方向 有人能帮忙吗Wpf Caliburn Micro自替换视图/视图模型,wpf,caliburn.micro,Wpf,Caliburn.micro,我有一个列表框来选择要编辑的项目。我还有一个编辑按钮。将其称为MainView[模型] 如果我按下编辑按钮,MainView[模型]将被EditView[模型]替换。 编辑视图不得显示在主视图下方或旁边的区域。它应该被完全替换,或者至少完全隐藏主视图 如果编辑完成(确定,取消),应再次显示主视图 我试图覆盖ContentControl,但没有成功。 现在,我在考虑一种NavigatorViewModel,它有一个属性公开的多个ViewModel。但我不确定这是不是正确的方向 有人能帮忙吗 Thx
Thx。您最好使用Caliburn.Micro提供的导体图案。导体管理一个或多个屏幕并控制其寿命。有关更多信息,请参阅
Conductor.Collection.OneActive
,这意味着它包含一个屏幕列表,其中一个屏幕可以同时处于活动状态:
public interface IShell
{
void ActivateItem(Screen screen);
}
public class ShellViewModel : Conductor<Screen>.Collection.OneActive, IShell
{
public ShellViewModel()
{
this.ActivateItem(new MainViewModel());
}
}
MainViewModel
可以使用其父对象shell导航到EditViewModel
:
public class MainViewModel : Screen
{
public void Edit()
{
((IShell)this.Parent).ActivateItem(new EditViewModel());
}
}
Edit
方法:
<!-- MainView.xaml -->
<UserControl x:Class="WpfApplication1.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button Name="Edit" Content="Edit" />
</UserControl>
TryClose
方法,这样视图模型就会自动关闭并从shell的项中删除。最后激活的项目(MainViewModel
)将被重新激活:
<!-- EditView.xaml -->
<UserControl x:Class="WpfApplication1.EditView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button Name="TryClose" Content="Back" />
</UserControl>
仅此而已。这只是视图切换的一个示例。它可以是完全不同的视图/视图模型配对。您只需简单地找出一种方法来传递要编辑的项目的索引或id即可。太棒了!谢谢!
public class EditViewModel : Screen
{
}
<!-- EditView.xaml -->
<UserControl x:Class="WpfApplication1.EditView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button Name="TryClose" Content="Back" />
</UserControl>