I made a WPF app that opens successive windows based on user interaction. For example, the first winodw asks what module the user wants to work on, when a selection is made and a button pushed, a new windows open showing some Vendors and summary counts. Selecting one of those vendors and pushing the Edit button opens another window that shows the details of that Vendor. Clicking a detail and then a button open yet another window where the user can change some numbers etc. Then the user closes that window, picks a different item and edits, or closes that window and picks a different vendor etc. Each window has its own view model currently. I want to get rid of all the layers of windows. Tab Control doesn't seem to be a good option, since the user will have to go through the correct sequences etc. What is the best way to change this to use only one window, and swap out what the user sees in the one window when he, for example, pushes a button to edit etc.?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Personally I prefer to use a <ContentControl />
for my content area, and to swap out the active content based on the user's current "window"
I have an example on my blog that you could look at, but the basic components look like this:
ViewModel:
ObservableCollection<IViewModel> AvailableScreens;
IViewModel ActiveScreen;
ICommand SetActiveScreenCommand;
With some XAML that looks like this:
<ContentControl Content="{Binding ActiveScreen}" />
And I usually use DataTemplates
to tell WPF how to draw each ViewModel
<Window.Resources>
<DataTemplate DataType="{x:Type local:ModulesViewModel}">
<local:ModulesView />
</DataTemplate>
<DataTemplate DataType="{x:Type local:VendorsViewModel}">
<local:VendorView />
</DataTemplate>
<DataTemplate DataType="{x:Type local:EditVendorViewModel}">
<local:EditVendorView />
</DataTemplate>
</Window.Resources>
回答2:
You can use a docking framework such as AvalonDock, which mimics the behavior of Visual Studio.