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

728x90
반응형
728x170

TestProject.zip
다운로드

▶ 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:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    Width="800"
    Height="600"
    Title="GridControl 클래스 : 행 선택 구현하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="CellDataTemplateKey">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*"    />
                    </Grid.ColumnDefinitions>
                    <Image x:Name="image" Grid.Column="0"
                        Margin="1"
                        Stretch="None"
                        Source="Image/cart.png" />
                    <TextBlock Grid.Column="1"
                        Margin="1"
                        Text="{Binding Path=Value}" />
                </Grid>
                <DataTemplate.Triggers>
                    <DataTrigger
                        Binding="{Binding Path=Data.Selected}"
                        Value="False">
                        <Setter
                            TargetName="image"
                            Property="Visibility"
                            Value="Collapsed" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
            <DataTemplate x:Key="TotalSummaryDataTemplateKey">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20" />
                        <ColumnDefinition Width="*"  />
                    </Grid.ColumnDefinitions>
                    <Image Grid.Column="0"
                        Margin="1"
                        Stretch="None"
                        Source="Image/rectangle.png" />
                    <TextBlock Grid.Column="1"
                        Margin="1"
                        Text="{Binding Path=Column.TotalSummaryText}" />
                </Grid>
            </DataTemplate>
        </Grid.Resources>
        <dxg:GridControl x:Name="gridControl"
            CustomUnboundColumnData="gridControl_CustomUnboundColumnData">
            <dxg:GridControl.Columns>
                <dxg:GridColumn
                    FieldName="ProductName"
                    CellTemplate="{StaticResource CellDataTemplateKey}" />
                <dxg:GridColumn FieldName="UnitPrice" />
                <dxg:GridColumn
                    FieldName="Selected"
                    UnboundType="Boolean" />
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView x:Name="tableView"
                    ShowGroupPanel="False"
                    NavigationStyle="Row"
                    ShowTotalSummary="True"
                    TotalSummaryItemTemplate="{StaticResource TotalSummaryDataTemplateKey}"
                    MouseDoubleClick="tableView_MouseDoubleClick" />
            </dxg:GridControl.View>
            <dxg:GridControl.TotalSummary>
                <dxg:GridSummaryItem
                    FieldName="UnitPrice"
                    SummaryType="Custom" />
            </dxg:GridControl.TotalSummary>
        </dxg:GridControl>
    </Grid>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

using DevExpress.Data;
using DevExpress.Xpf.Grid;

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

        #region Field

        /// <summary>
        /// 언바운드 데이터 리스트
        /// </summary>
        private List<bool> unboundDataList = new List<bool>();

        #endregion

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

        #region 생성자 - MainWindow()

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

            Loaded += Window_Loaded;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.gridControl.ItemsSource = new NorthwindEntities().Products;

            SetUnboundDataSource(this.gridControl.VisibleRowCount);

            this.gridControl.CustomSummary += gridControl_CustomSummary;
        }

        #endregion
        #region 그리드 컨트롤 커스텀 요약 처리하기 - gridControl_CustomSummary(sender, e)

        /// <summary>
        /// 그리드 컨트롤 커스텀 요약 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void gridControl_CustomSummary(object sender, CustomSummaryEventArgs e)
        {
            if(e.SummaryProcess == CustomSummaryProcess.Start)
            {
                e.TotalValue = 0m;
            }

            if(e.SummaryProcess == CustomSummaryProcess.Calculate)
            {
                if((bool)e.GetValue("Selected"))
                {
                    e.TotalValue = Convert.ToDecimal(e.TotalValue) + Convert.ToDecimal(e.GetValue("UnitPrice"));
                }
            }
        }

        #endregion
        #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")
            {
                if(e.IsGetData)
                {
                    e.Value = this.unboundDataList[e.ListSourceRowIndex];
                }

                if(e.IsSetData)
                {
                    this.unboundDataList[e.ListSourceRowIndex] = (bool)e.Value;
                }
            }
        }

        #endregion
        #region 테이블 뷰 마우스 더블 클릭시 처리하기 - tableView_MouseDoubleClick(sender, e)

        /// <summary>
        /// 테이블 뷰 마우스 더블 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void tableView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            int rowHandle = this.gridControl.View.GetRowHandleByMouseEventArgs(e);

            if(this.gridControl.IsValidRowHandle(rowHandle))
            {
                this.gridControl.SetCellValue(rowHandle, "Selected", !(bool)this.gridControl.GetCellValue(rowHandle, "Selected"));

                this.gridControl.RefreshData();
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 언바운드 데이터 소스 설정하기 - SetUnboundDataSource(count)

        /// <summary>
        /// 언바운드 데이터 소스 설정하기
        /// </summary>
        /// <param name="count">카운트</param>
        private void SetUnboundDataSource(int count)
        {
            for(int i = 0; i < count; i++)
            {
                this.unboundDataList.Add(false);
            }
        }

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

댓글을 달아 주세요