我建议主向导viewModel有一个steps
视图模型的集合并处理它们之间的导航。在浏览它应该调用的验证方法中step
的ViewModels:
WizardVM:
public class WizardVM
{
// this commands should support CanExecute
public ICommand GotoNextCommand { get; private set; } // should open next step VM
public ICommand GotoBackCommand { get; private set; } // should open previous step VM
// this prop should be set by 'GotoNext', 'GotoBack' commands
public object CurrentStep { get; private set; }
// probably internally you will have a list of all steps:
private ICollection<object> _stepViewModels = ...;
}
WizardView:
<StackPanel>
<ContentPresenter Content="{Binding CurrentStep}">
<StackPanel Orientation="Horizontal">
<Button Command="{Binding GotoBackCommand}">Back</Button>
<Button Command="{Binding GotoNextCommand}">Next</Button>
</StackPanel>
</StackPanel>
UPDATE:
视图可以用的ViewModels经由Datatemplating
耦合。例如添加到这个资源App.Xaml
:
<DataTemplate DataType="{x:Type local:Step1ViewModel}">
<Step1View />
</DateTemplate>
<DataTemplate DataType="{x:Type local:Step2ViewModel}">
<Step2View />
</DateTemplate>
你的ViewModels应该知道绝对没有关于意见。这意味着WizardVM
应该 只公开其他viewModels但不公开视图。这是MVVM的经验法则。
UPDATE2糟糕,我忘了Silverlight还没有DataTemplating。在Silverlight中,我仍然会公开ViewModels,但使用转换器将它们绑定到ContentPresenters
,转换器会将viewModel转换为相应的视图。
哇,这很快! 但是......我很好奇,你认为CurrentStep属性应该是可见的东西(I.E一个视图)?否则,我不会看到ContentPresenter如何呈现它。我的问题是将包含的视图和视图模型与海誓山盟联系起来。 – 2011-03-01 22:08:43
@Per,'CurrentStep'应该公开Step ViewModel。查看更新后的答案 – Snowbear 2011-03-01 22:14:23
好的...使用值转换器将视图模型转换为视图。这是相当操纵,ehrm ..我的意思是务实的:)但不过,我没有想到一个新的方法!我怀疑这是我将要去做的事情,但是我将授予你创新的答案,甚至超出我自己的(而非操纵性的)本质......感谢您花时间回复! – 2011-03-02 06:20:02