안녕하세요. 아래에서 문의를 드렸었는데 코드까지 송부하여 아래와 같이 다시 문의드립니다.
제가 구현하고자 하는 것은 버튼 클릭 시, View가 전환되는 것입니다.
.
.
.
App.xaml의 경우 ↓
<Application x:Class="NavigationView.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NavigationView"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Mapping.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
.
.
.
Mapping.xaml의 경우 ↓
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:NavigationView.View"
xmlns:vm="clr-namespace:NavigationView.ViewModel">
<DataTemplate DataType="{x:Type vm:RedViewModel}">
<v:RedUserControl/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:BlueViewModel}">
<v:BlueUserControl/>
</DataTemplate>
</ResourceDictionary>
.
.
.
MainWindow.xaml의 경우 ↓
<Window x:Class="NavigationView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NavigationView.View"
xmlns:vm="clr-namespace:NavigationView.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<vm:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Content="Red UserControl" Command="{Binding RedCommand}"/>
<Button Content="Blue UserControl" Command="{Binding BlueCommand}"/>
</StackPanel>
<ContentControl Grid.Row="1" Content="{Binding ContentView}"/>
</Grid>
</Window>
.
.
.
MainWindowViewModel.cs의 경우 ↓
using NavigationView.Core;
using NavigationView.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace NavigationView.ViewModel
{
public class MainWindowViewModel : ViewModelBase
{
private ViewModelManager _viewModelManager;
private object _contentView;
public object ContentView
{
get { return _contentView; }
set { _contentView = value; OnPropertyChanged(); }
}
public ICommand RedCommand { get; set; }
public void Red(object obj)
{
ContentView = _viewModelManager._redViewModel;
}
public ICommand BlueCommand { get; set; }
public void Blue(object obj)
{
ContentView = _viewModelManager._blueViewModel;
}
public MainWindowViewModel()
{
_viewModelManager = new ViewModelManager();
RedCommand = new RelayCommand<object>(Red);
BlueCommand = new RelayCommand<object>(Blue);
ContentView = _viewModelManager._redViewModel;
}
}
}
.
.
.
RedUserControl.xaml의 경우 ↓
<UserControl x:Class="NavigationView.View.RedUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:NavigationView.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Background="Red">
<Grid>
</Grid>
</UserControl>
.
.
.
RedViewModel.cs의 경우 ↓
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NavigationView.ViewModel
{
public class RedViewModel
{
public RedViewModel()
{
}
}
}
.
.
.
BlueUserControl.xaml의 경우 ↓
<UserControl x:Class="NavigationView.View.BlueUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:NavigationView.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Background="Blue">
<Grid>
</Grid>
</UserControl>
.
.
.
BlueViewModel.cs의 경우 ↓
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NavigationView.ViewModel
{
public class BlueViewModel
{
public BlueViewModel()
{
}
}
}
.
.
.
ViewModelManager.cs의 경우 ↓
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NavigationView.ViewModel
{
public class ViewModelManager
{
public RedViewModel _redViewModel;
public BlueViewModel _blueViewModel;
public ViewModelManager()
{
_redViewModel = new RedViewModel();
_blueViewModel = new BlueViewModel();
}
}
}
여기까지 작성을 했고 ViewModel을 전환 시 View가 재생성이 되는 현상이 발생되어 일전에 말씀하신 블로그를 참고해봤는데 제가 지식이 좀 부족하여 계속 막혀 진행이 되지 않고 있습니다ㅠ
이부분만 뚫리지 않아 고민하던차에 문의드리게 되었습니다.
감사합니다.