■ ViewModelExtensions 클래스의 Parameter 첨부 속성을 사용해 뷰 모델 간 데이터를 공유하는 방법을 보여준다.
▶ User.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 |
namespace TestProject { /// <summary> /// 사용자 /// </summary> public class User { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 사용자 배열 /// </summary> public static User[] UserArray = new User[] { new User() { FirstName = "Nancy" , LastName = "Davolio" , Country = "USA" , City = "Los Angeles"}, new User() { FirstName = "Andrew", LastName = "Fuller" , Country = "UK" , City = "London" }, new User() { FirstName = "Laura" , LastName = "Callahan", Country = "Australia" , City = "Sydney" }, }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get; set; } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get; set; } #endregion #region 전체 이름 - FullName /// <summary> /// 전체 이름 /// </summary> public string FullName { get { return FirstName + " " + LastName; } } #endregion #region 국가 - Country /// <summary> /// 국가 /// </summary> public string Country { get; set; } #endregion #region 도시 - City /// <summary> /// 도시 /// </summary> public string City { get; set; } #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 = pParameter 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<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> |
▶ 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 89 90 |
using System; 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 } } |
▶ 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 37 |
<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" Title="Loosely Coupled View Model" Width="600" Height="450" 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> |