안녕하세요.
각각의 버튼을 클릭했을 때, 뷰가 전환되는 간단한 예제 프로그램을 알고싶습니다.
우선은 제 생각대로 공부한대로 작성해봤는데 잘 되지 않아 문의드립니다.
Page1Model
namespace Step3_1.Models.Page1
{
class Page1Model
{
public string name { get; set; }
public int age { get; set; }
}
}
Page2Model
namespace Step3_1.Models.Page2
{
class Page2Model
{
public string message { get; set; }
}
}
Page1VM
namespace Step3_1.ViewModels.Page1
{
class Page1VM : ViewModelBase
{
private Models.Page1.Page1Model _page1Model;
public string Name
{
get { return _page1Model.name; }
set { _page1Model.name = value; OnPropertyChanged(); }
}
public int age
{
get { return _page1Model.age; }
set { _page1Model.age = value; OnPropertyChanged(); }
}
public Page1VM()
{
InitializeVM();
}
private void InitializeVM()
{
_page1Model = new Models.Page1.Page1Model();
}
}
}
Page2VM
namespace Step3_1.ViewModels.Page2
{
class Page2VM :ViewModelBase
{
private Models.Page2.Page2Model _page2Model;
public string Message
{
get { return _page2Model.message; }
set { _page2Model.message = value; OnPropertyChanged(); }
}
public Page2VM()
{
InitializeVM();
}
private void InitializeVM()
{
_page2Model = new Models.Page2.Page2Model();
}
}
}
Page1View
<UserControl x:Class="Step3_1.VIews.Page1.Page1View"
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:Step3_1.VIews.Page1"
xmlns:vm="clr-namespace:Step3_1.ViewModels.Page1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<vm:Page1VM/>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="AntiqueWhite" Orientation="Horizontal">
<TextBlock Text="Name : "/>
<TextBox Text="{Binding Name}" Width="100"/>
</StackPanel>
<StackPanel Grid.Row="1" Background="Gainsboro" Orientation="Horizontal">
<TextBlock Text="Age : "/>
<TextBox Text="{Binding age}" Width="100"/>
</StackPanel>
</Grid>
</UserControl>
Page2View
<UserControl x:Class="Step3_1.VIews.Page2.Page2View"
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:Step3_1.VIews.Page2"
xmlns:vm="clr-namespace:Step3_1.ViewModels.Page2"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<vm:Page2VM/>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="Aquamarine" Orientation="Horizontal">
<TextBlock Text="Message : "/>
<TextBox Text="{Binding Message}" Width="100"/>
</StackPanel>
</Grid>
</UserControl>
MainWindowVM
namespace Step3_1.ViewModels
{
public class MainWindowVM : ViewModelBase
{
private object _currentVM;
public object CurrentVM
{
get { return _currentVM; }
set { _currentVM = value; OnPropertyChanged(); }
}
private Page1.Page1VM _page1VM;
private Page2.Page2VM _page2VM;
public ICommand Page1Command { get; set; }
private void Page1(object obj)
{
CurrentVM = _page1VM;
}
public ICommand Page2Command { get; set; }
private void Page2(object obj)
{
CurrentVM = _page2VM;
}
public MainWindowVM()
{
InitializeVM();
SwitchToFirstView();
}
private void InitializeVM()
{
_page1VM = new Page1.Page1VM();
_page2VM = new Page2.Page2VM();
Page1Command = new RelayCommand<object>(Page1);
Page2Command = new RelayCommand<object>(Page2);
}
private void SwitchToFirstView()
{
CurrentVM = _page1VM;
}
}
}
MainWindow
<Window x:Class="Step3_1.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:Step3_1"
xmlns:vm="clr-namespace:Step3_1.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<vm:MainWindowVM/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Background="Beige" Orientation="Horizontal">
<Button Content="Page1" Width="100" Command="{Binding Page1Command}"/>
<Button Content="Page2" Width="100" Command="{Binding Page2Command}"/>
</StackPanel>
<DockPanel Grid.Row="1">
<ContentControl Content="{Binding CurrentVM}"/>
</DockPanel>
</Grid>
</Window>
- View를 볼수가 없는데 방법이 있을까요?
- View를 Switching(Navigation)하는 가장 범용적이고 좋은 코드(방법)이 있을까요?
감사합니다.