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

TestProject.zip
다운로드

▶ MainViewModel.cs

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
    }
}

 

728x90

 

▶ DetailViewModel.cs

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
    }
}

 

300x250

 

▶ DetailView.xaml

<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

<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>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요