첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ SelectionHelper.cs

using System.Collections.Generic;
using System.Text;

namespace TestProject
{
    /// <summary>
    /// 선택 도우미
    /// </summary>
    /// <typeparam name="KeyType">키 타입</typeparam>
    public class SelectionHelper<KeyType>
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 사전
        /// </summary>
        private Dictionary<KeyType, bool> dictionary = new Dictionary<KeyType, bool>();

        #endregion

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

        #region 선택 여부 구하기 - GetIsSelected(key)

        /// <summary>
        /// 선택 여부 구하기
        /// </summary>
        /// <param name="key">키</param>
        /// <returns>선택 여부</returns>
        public bool GetIsSelected(KeyType key)
        {
            bool isSelected;

            if(this.dictionary.TryGetValue(key, out isSelected))
            {
                return isSelected;
            }

            return false;
        }

        #endregion
        #region 선택 여부 설정하기 - SetIsSelected(key, value)

        /// <summary>
        /// 선택 여부 설정하기
        /// </summary>
        /// <param name="key">키</param>
        /// <param name="value">값</param>
        public void SetIsSelected(KeyType key, bool value)
        {
            if(value)
            {
                this.dictionary[key] = value;
            }
            else
            {
                this.dictionary.Remove(key);
            }
        }

        #endregion
        #region 선택 키 리스트 구하기 - GetSelectedKeyList()

        /// <summary>
        /// 선택 키 리스트 구하기
        /// </summary>
        /// <returns>선택 키 리스트</returns>
        public List<KeyType> GetSelectedKeyList()
        {
            List<KeyType> keyList = new List<KeyType>();

            foreach(KeyType key in this.dictionary.Keys) 
            {
                keyList.Add(key);
            }

            return keyList;
        }

        #endregion
        #region 선택 키 리스트에서 문자열 구하기 - GetStringFromSelectedKeyList()

        /// <summary>
        /// 선택 키 리스트에서 문자열 구하기
        /// </summary>
        /// <returns>문자열</returns>
        public string GetStringFromSelectedKeyList()
        {
            List<KeyType> keyList = GetSelectedKeyList();

            StringBuilder stringBuilder = new StringBuilder();

            for(int i = 0; i < keyList.Count; i++)
            {
                stringBuilder.AppendLine(keyList[i].ToString());
            }

            return stringBuilder.ToString();
        }

        #endregion
        #region 선택 카운트 구하기 - GetSelectionCount()

        /// <summary>
        /// 선택 카운트 구하기
        /// </summary>
        /// <returns>선택 카운트</returns>
        public int GetSelectionCount()
        {
            return this.dictionary.Count;
        }

        #endregion
    }
}

 

728x90

 

▶ 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"
    Width="800"
    Height="600"
    Title="GridControl 클래스 : 언바운드 편집 가능한 체크 컬럼 구현하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"    />
        </Grid.RowDefinitions>
        <Button Grid.Row="0"
            Content="Invert Selection"
            Click="invertSelectionButton_Click" />
        <Button Grid.Row="1"
            Margin="0 10 0 0"
            Content="Get Selection"
            Click="getSelectionButton_Click" />
        <dxg:GridControl x:Name="gridControl" Grid.Row="2"
            Margin="0 10 0 0"
            CustomUnboundColumnData="gridControl_CustomUnboundColumnData"
            ItemsSource="{Binding Data}">
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="ID"     />
                <dxg:GridColumn FieldName="Number" />
                <dxg:GridColumn
                    FieldName="Selected"
                    UnboundType="Boolean"
                    AllowSorting="False">
                    <dxg:GridColumn.CellTemplate>
                        <DataTemplate>
                            <dxe:CheckEdit
                                IsChecked="{Binding Data.Selected}"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center" />
                        </DataTemplate>
                    </dxg:GridColumn.CellTemplate>
                </dxg:GridColumn>
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView x:Name="tableView" />
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</Window>

 

300x250

 

▶ MainWindow.xaml.cs

using System;
using System.Windows;

using DevExpress.Xpf.Grid;

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

        #region Field

        /// <summary>
        /// 샘플 리스트
        /// </summary>
        private SampleList sampleList = new SampleList();

        /// <summary>
        /// 선택 도우미
        /// </summary>
        private SelectionHelper<Guid> selectionHelper = new SelectionHelper<Guid>();

        #endregion

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

        #region 메인 윈도우 - MainWindow()

        /// <summary>
        /// 메인 윈도우
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this.sampleList;
        }

        #endregion

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

        #region 그리드 컨트롤 커스텀 언바운드 컬럼 데이터 처리하기 - gridControl_CustomUnboundColumnData(sender, e)

        /// <summary>
        /// 그리드 컨트롤 커스텀 언바운드 컬럼 데이터 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void gridControl_CustomUnboundColumnData(object sender, GridColumnDataEventArgs e)
        {
            if(e.Column.FieldName != "Selected")
            {
                return;
            }

            Guid keyGuid = (Guid)e.GetListSourceFieldValue("ID");

            if(e.IsGetData)
            {
                e.Value = this.selectionHelper.GetIsSelected(keyGuid);
            }

            if(e.IsSetData)
            {
                this.selectionHelper.SetIsSelected(keyGuid, (bool)e.Value);
            }
        }

        #endregion
        #region Invert Selection 버튼 클릭시 처리하기 - invertSelectionButton_Click(sender, e)

        /// <summary>
        /// Invert Selection 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void invertSelectionButton_Click(object sender, RoutedEventArgs e)
        {
            for(int i = 0; i < this.sampleList.Data.Count; i++)
            {
                int rowHandle = this.gridControl.GetRowHandleByListIndex(i);

                bool newIsSelected = !this.selectionHelper.GetIsSelected(this.sampleList.Data[i].ID);

                this.gridControl.SetCellValue(rowHandle, "Selected", newIsSelected);
            }
        }

        #endregion
        #region Get Selection 버튼 클릭시 처리하기 - getSelectionButton_Click(sender, e)

        /// <summary>
        /// Get Selection 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void getSelectionButton_Click(object sender, RoutedEventArgs e)
        {
            string caption = string.Format("Selected rows (Total: {0})", this.selectionHelper.GetSelectionCount());

            MessageBox.Show(this.selectionHelper.GetStringFromSelectedKeyList(), caption);
        }

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

댓글을 달아 주세요