■ 느슨하게 결합된 뷰 모델을 사용하는 방법을 보여준다.
▶ MainViewModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
using System.Collections.Generic; using System.Linq; using DevExpress.Xpf.Mvvm; namespace TestProject { /// <summary> /// 메인 뷰 모델 /// </summary> public class MainViewModel : ViewModelBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 사용자 열거 가능 /// </summary> private IEnumerable<User> userEnumerable; /// <summary> /// 선택된 사용자 /// </summary> private User selectedUser; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 사용자 열거 가능 - UserEnumerable /// <summary> /// 사용자 열거 가능 /// </summary> public IEnumerable<User> UserEnumerable { get { return this.userEnumerable; } set { SetProperty(ref this.userEnumerable, value, () => UserEnumerable); } } #endregion #region 선택된 사용자 - SelectedUser /// <summary> /// 선택된 사용자 /// </summary> public User SelectedUser { get { return this.selectedUser; } set { SetProperty(ref this.selectedUser, value, () => SelectedUser); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainViewModel() /// <summary> /// 생성자 /// </summary> public MainViewModel() { UserEnumerable = User.UserArray; SelectedUser = UserEnumerable.FirstOrDefault(); } #endregion } } |
▶ DetailViewModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
using System.Linq; using DevExpress.Xpf.Mvvm; namespace TestProject { /// <summary> /// 상세 뷰 모델 /// </summary> public class DetailViewModel : ViewModelBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 사용자 /// </summary> private User user; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 사용자 - User /// <summary> /// 사용자 /// </summary> public User User { get { return this.user; } set { SetProperty(ref this.user, value, () => User); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DetailViewModel() /// <summary> /// 생성자 /// </summary> public DetailViewModel() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 매개 변수 변경시 처리하기 - OnParameterChanged(parameter) /// <summary> /// 매개 변수 변경시 처리하기 /// </summary> /// <param name="parameter">매개 변수</param> protected override void OnParameterChanged(object parameter) { if(IsInDesignMode) { User = User.UserArray.FirstOrDefault(); } else { User = parameter as User; } } #endregion } } |
▶ DetailView.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<UserControl x:Class="TestProject.DetailView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" DataContext="{DynamicResource DetailViewModelKey}"> <UserControl.Resources> <local:DetailViewModel x:Key="DetailViewModelKey" /> <Style TargetType="{x:Type Label}"> <Setter Property="FontSize" Value="16" /> </Style> </UserControl.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Margin="0 0 5 0" Content="First Name" /> <Label Grid.Row="0" Grid.Column="1" Width="160" Content="{Binding User.FirstName}" /> <Label Grid.Row="1" Grid.Column="0" Margin="0 0 5 0" Content="Last Name" /> <Label Grid.Row="1" Grid.Column="1" Width="160" Content="{Binding User.LastName}" /> <Label Grid.Row="2" Grid.Column="0" Margin="0 0 5 0" Content="Country" /> <Label Grid.Row="2" Grid.Column="1" Width="160" Content="{Binding User.Country}" /> <Label Grid.Row="3" Grid.Column="0" Margin="0 0 5 0" Content="City" /> <Label Grid.Row="3" Grid.Column="1" Content="{Binding User.City}" /> </Grid> </UserControl> |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" Width="800" Height="600" Title="느슨하게 결합된 뷰 모델 사용하기" FontFamily="나눔고딕코딩" FontSize="16" DataContext="{DynamicResource MainViewModelKey}"> <Window.Resources> <local:MainViewModel x:Key="MainViewModelKey" /> </Window.Resources> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ListBox x:Name="listBox" Grid.Row="0" Height="90" FontSize="16" ItemsSource="{Binding UserEnumerable}" SelectedItem="{Binding SelectedUser}" DisplayMemberPath="FullName" /> <Border Grid.Row="1" Margin="0 10 0 0" BorderThickness="1" BorderBrush="Black"> <local:DetailView x:Name="detailView" dxm:ViewModelExtensions.Parameter="{Binding SelectedUser, Source={StaticResource MainViewModelKey}}" /> </Border> </Grid> </Window> |