我建議主嚮導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