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

TestProject.zip
다운로드

▶ CreateNewRowWindow.xaml

<Window x:Class="TestProject.CreateNewRowWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    SizeToContent="WidthAndHeight"
    Title="Create New Row"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Margin"            Value="0 3 0 3"/>
        </Style>
    </Window.Resources>
    <Grid Margin="10">
        <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>
        <TextBlock
            Grid.Row="0"
            Grid.Column="0"
            Text="Text1 : " />
        <TextBox x:Name="Text1"
            Grid.Row="0"
            Grid.Column="1"
            MinWidth="100" />
        <TextBlock Text="Text2 : "
            Grid.Row="1"
            Grid.Column="0" />
        <TextBox x:Name="Text2"
            Grid.Row="1"
            Grid.Column="1"
            MinWidth="100" />
        <TextBlock
            Grid.Row="2"
            Grid.Column="0"
            Text="Text3 : " />
        <TextBox x:Name="Text3"
            Grid.Row="2"
            Grid.Column="1"
            MinWidth="100" />
        <StackPanel
            VerticalAlignment="Bottom"
            HorizontalAlignment="Right"
            Orientation="Horizontal"
            Grid.Row="3"
            Grid.Column="1">
            <Button
                Margin="5"
                Padding="3"
                IsDefault="True"
                Content="OK"
                Click="button_Click" />
            <Button
                Margin="5"
                Padding="3"
                IsCancel="True"
                Content="Cancel" />
        </StackPanel>
    </Grid>
</Window>

 

728x90

 

▶ CreateNewRowWindow.xaml.cs

using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 신규 행 생성 윈도우
    /// </summary>
    public partial class CreateNewRowWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - CreateNewRowWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public CreateNewRowWindow()
        {
            InitializeComponent();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 버튼 클릭시 처리하기 - button_Click(sender, e)

        /// <summary>
        /// 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void button_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;

            Close();
        }

        #endregion
    }
}

 

300x250

 

▶ MainViewModel.cs

using System;
using System.ComponentModel;

namespace TestProject
{
    /// <summary>
    /// 메인 뷰 모델
    /// </summary>
    public class MainViewModel : INotifyPropertyChanged
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Event
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 속성 변경시 - PropertyChanged

        /// <summary>
        /// 속성 변경시
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 샘플 바인딩 리스트
        /// </summary>
        private BindingList<Sample> sampleBindingList;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 샘플 바인딩 리스트 - SampleBindingList

        /// <summary>
        /// 샘플 바인딩 리스트
        /// </summary>
        public BindingList<Sample> SampleBindingList
        {
            get
            {
                return this.sampleBindingList;
            }
            set
            {
                if(value == this.sampleBindingList)
                {
                    return;
                }

                this.sampleBindingList = value;

                OnPropertyChanged("SampleBindingList");
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainViewModel()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainViewModel()
        {
            SampleBindingList = CreateSampleBindingList();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 신규 행 추가하기 - AddNewRow(text1, text2, text3)

        /// <summary>
        /// 신규 행 추가하기
        /// </summary>
        /// <param name="text1">텍스트 1</param>
        /// <param name="text2">텍스트 2</param>
        /// <param name="text3">텍스트 3</param>
        public void AddNewRow(string text1, string text2, string text3)
        {
            SampleBindingList.Add(CreateSample(text1, text2, text3));
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 샘플 바인딩 리스트 생성하기 - CreateSampleBindingList()

        /// <summary>
        /// 샘플 바인딩 리스트 생성하기
        /// </summary>
        /// <returns>샘플 바인딩 리스트</returns>
        private BindingList<Sample> CreateSampleBindingList()
        {
            BindingList<Sample> bindingList = new BindingList<Sample>();

            for(int i = 0; i < 5; i++)
            {
                bindingList.Add(CreateSample(Guid.NewGuid().ToString(), "text2 " + i, "text3 " + i));
            }

            return bindingList;
        }

        #endregion
        #region 샘플 생성하기 - CreateSample(text1, text2, text3)

        /// <summary>
        /// 샘플 생성하기
        /// </summary>
        /// <param name="text1">텍스트 1</param>
        /// <param name="text2">텍스트 2</param>
        /// <param name="text3">텍스트 3</param>
        /// <returns>샘플</returns>
        private Sample CreateSample(string text1, string text2, string text3)
        {
            return new Sample() { Text1 = text1, Text2 = text2, Text3 = text3 };
        }

        #endregion
        #region 속성 변경시 처리하기 - OnPropertyChanged(propertyName)

        /// <summary>
        /// 속성 변경시 처리하기
        /// </summary>
        /// <param name="propertyName">속성명</param>
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

 

▶ 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:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="다른 모달 윈도우를 사용해 신규 행 추가하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Button x:Name="addNewRowButton"
            Margin="0 5 0 5"
            Focusable="False"
            Content="Add new row..."
            Click="addNewRowButton_Click" />
        <dxg:GridControl x:Name="gridControl"
            Grid.Row="1"
            ItemsSource="{Binding SampleBindingList}"
            AutoGenerateColumns="AddNew">
            <dxg:GridControl.View>
                <dxg:TableView x:Name="tableView" />
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 메인 뷰 모델 - MainViewModel

        /// <summary>
        /// 메인 뷰 모델
        /// </summary>
        public MainViewModel MainViewModel
        {
            get
            {
                return DataContext as MainViewModel;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Add New Row 버튼 클릭시 처리하기 - addNewRowButton_Click(sender, e)

        /// <summary>
        /// Add New Row 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void addNewRowButton_Click(object sender, RoutedEventArgs e)
        {
            CreateNewRowWindow createNewRowWindow = new CreateNewRowWindow();

            createNewRowWindow.ShowDialog();

            if(createNewRowWindow.DialogResult.Value == true)
            {
                MainViewModel.AddNewRow
                (
                    createNewRowWindow.Text1.Text,
                    createNewRowWindow.Text2.Text,
                    createNewRowWindow.Text3.Text
                );

                this.tableView.MoveLastRow();

                this.gridControl.Focus();
            }
        }

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

댓글을 달아 주세요