첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ User.cs

namespace HowToPassDataBetweenLooselyCoupledViewModels
{
    /// <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
    }
}

 

728x90

 

▶ DetailViewModel.cs

using System.Linq;

using DevExpress.Xpf.Mvvm;

namespace HowToPassDataBetweenLooselyCoupledViewModels
{
    /// <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
    }
}

 

300x250

 

▶ DetailView.xaml

<UserControl
    x:Class="HowToPassDataBetweenLooselyCoupledViewModels.DetailView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:HowToPassDataBetweenLooselyCoupledViewModels"
    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

using System;
using System.Collections.Generic;
using System.Linq;

using DevExpress.Xpf.Mvvm;

namespace HowToPassDataBetweenLooselyCoupledViewModels
{
    /// <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

<Window
    x:Class="HowToPassDataBetweenLooselyCoupledViewModels.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:HowToPassDataBetweenLooselyCoupledViewModels"
    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>

 

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요