하나의 창을 포함하는 WPF 응용 프로그램이 있습니다. 이 창만 던져서 사용자가 앱에서 내비게이션을 수행 할 수 있습니다.WPF MVVM : MainWindow 탐색
어플리케이션 구조이다
- MainWindow.xaml
- MainWindowViewModel.cs
- StartPage.xaml
- StartPageViewMode.cs
- Systems.xaml
- Systems.cs
- 다른보기 및 관련보기 모델.
MainWindow.xaml
<Grid>
<ContentControl Content="{Binding CurrentWorkspace}" x:Name="ContentControlMainWindow" VerticalAlignment="Stretch"/>
</Grid>
MainWindowViewModel.cs
private ContentControl _currentWorkspace;
public ContentControl CurrentWorkspace
{
get => _currentWorkspace;
set => SetProperty(ref _currentWorkspace, value);
}
//c'tor
public MainWindowViewModel()
{
CurrentWorkspace.Content = new ContentControl { Content = new StartPage()
}
당신이 볼 수 있듯이, 응용 프로그램 초기화에서 내가 CurrentWorkspace에 시작 페이지보기를로드하고 있습니다. StartPageViewModel에서 CurrentWorkspace 컨텐트를 다른 뷰로 변경해야합니다. 기본적으로 나는이 CurrentWorkspace를 응용 프로그램의 각 부분에서 제어 (및 변경)하는 데 어려움을 겪고 있습니다.
MainWindowViewModel.cs에서 : MainWindow.xaml에서
// You would more likely type this as something like ViewModelBase/ObservableObject/etc.
private object _currentWorkspace;
public object CurrentWorkspace
{
get => _currentWorkspace;
set => SetProperty(ref _currentWorkspace, value);
}
private StartPageViewModel _startPageViewModel;
public StartPageViewModel StartPageViewModel
{
get => _startPageViewModel;
set => SetProperty(ref _startPageViewModel, value);
}
private AnotherPageViewModel _anotherPageViewModel;
public AnotherPageViewModel AnotherPageViewModel
{
get => _anotherPageViewModel;
set => SetProperty(ref _anotherPageViewModel, value);
}
public MainWindowViewModel()
{
StartPageViewModel = new StartPageViewModel();
AnotherPageViewModel = new AnotherPageViewModel();
CurrentWorkspace = StartPageViewModel;
}
// Navigation Method
private void NavigateToStartPage()
{
if (CurrentWorkspace != StartPageViewModel)
CurrentWorkspace = StartPageViewModel;
}
// Navigation Method
private void NavigateToAnotherPage()
{
if (CurrentWorkspace != AnotherPageViewModel)
CurrentWorkspace = AnotherPageViewModel;
}
:
<Window ...
xmlns:vm="clr-namespace:App.ViewModels"
xmlns:vw="clr-namespace:App.Views"
... >
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
<Grid>
<ContentControl x:Name="ContentControlMainWindow"
Content="{Binding CurrentWorkspace}"
VerticalAlignment="Stretch">
<ContentControl.Resources>
<DataTemplate x:Key="start_page_view"
DataType="{x:Type vm:StartPageViewModel}">
<vw:StartPage />
</DataTemplate>
<DataTemplate x:Key="another_page_view"
DataType="{x:Type vm:AnotherPageViewModel}">
<vw:AnotherPage />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</Grid>
</Window/>
당신은 당신이 원하는대로에 CurrentWorkspace
을 설정할 수 있습니다
WPF의 탐색 개념을 참조하십시오. – Aybe