첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ GridControl 클래스 : GROUP SUMMARY를 위한 커스텀 인쇄 템플리트 제공하기

----------------------------------------------------------------------------------------------------


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:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"

    xmlns:dxp="http://schemas.devexpress.com/winfx/2008/xaml/printing"

    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="GROUP SUMMARY를 위한 커스텀 인쇄 템플리트 제공하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <DockPanel>

        <Button x:Name="showPrintPreview" DockPanel.Dock="Top"

            Margin="0 0 0 5"

            Click="showPrintPreview_Click">

            Show Print Preview

        </Button>

        <dxg:GridControl x:Name="gridControl"

            ItemsSource="{Binding}">

            <dxg:GridControl.Columns>

                <dxg:GridColumn FieldName="Number1" />

                <dxg:GridColumn FieldName="Number2" GroupIndex="0" />

                <dxg:GridColumn FieldName="Text1"   />

                <dxg:GridColumn FieldName="Text2"   />

            </dxg:GridControl.Columns>

            <dxg:GridControl.GroupSummary>

                <dxg:GridSummaryItem SummaryType="Count" />

                <dxg:GridSummaryItem

                    FieldName="Number2"

                    SummaryType="Sum" />

            </dxg:GridControl.GroupSummary>

            <dxg:GridControl.View>

                <dxg:TableView x:Name="tableView"

                    NavigationStyle="Row">

                    <dxg:TableView.PrintGroupRowTemplate>

                        <DataTemplate>

                            <StackPanel Orientation="Horizontal">

                                <dxg:CellFillControl>

                                    <dxg:CellFillControl.Template>

                                        <ControlTemplate>

                                            <dxe:TextEdit x:Name="PART_Edit"

                                                Style="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).PrintGroupRowStyle}"

                                                Margin="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).PrintDataIndentMargin}"

                                                Width="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).PrintDataIndent}"

                                                BorderThickness="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).PrintDataIndentBorderThickness}"

                                                Background="Transparent">

                                                <dxp:ExportSettings.OnPageUpdater>

                                                    <dxg:LastOnPageUpdater />

                                                </dxp:ExportSettings.OnPageUpdater>

                                            </dxe:TextEdit>

                                        </ControlTemplate>

                                    </dxg:CellFillControl.Template>

                                </dxg:CellFillControl>

                                <dxg:GroupRowContentControl>

                                    <dxg:GroupRowContentControl.Template>

                                        <ControlTemplate>

                                            <DockPanel Width="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).TotalHeaderWidth}">

                                                <dxe:TextEdit x:Name="PART_Edit"

                                                    DockPanel.Dock="Left"

                                                    Style="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).PrintGroupRowStyle}"

                                                    BorderThickness="1 0 0 1"

                                                    EditValue="{Binding Path=Content.GroupValue.DisplayText, Mode=OneWay}" />

                                                <local:CollectionConverterDecorator x:Name="collectionConverterDecorator"

                                                    Collection="{Binding Path=Content.GroupSummaryData, Mode=OneWay}">

                                                    <dxe:TextEdit

                                                        Style="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).PrintGroupRowStyle}"

                                                        HorizontalContentAlignment="Right"

                                                        BorderThickness="0 0 1 1"

                                                        EditValue="{Binding Path=SummaryText, Mode=OneWay, ElementName=collectionConverterDecorator}" />

                                                </local:CollectionConverterDecorator>

                                            </DockPanel>

                                        </ControlTemplate>

                                    </dxg:GroupRowContentControl.Template>

                                </dxg:GroupRowContentControl>

                            </StackPanel>

                        </DataTemplate>

                    </dxg:TableView.PrintGroupRowTemplate>

                </dxg:TableView>

            </dxg:GridControl.View>

        </dxg:GridControl>

    </DockPanel>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Collections.Generic;

using System.Windows;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            List<Sample> list = new List<Sample>();

 

            for(int i = 0; i < 100; i++)

            {

                list.Add

                (

                    new Sample()

                    {

                        Number1 = i,

                        Number2 = (i + 10) / 10,

                        Text1   = "Row " + i,

                        Text2   = "Row " + i

                    }

                );

            }

 

            DataContext = list;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

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

 

        #region Show Print Preview 버튼 클릭시 처리하기 - showPrintPreview_Click(sender, e)

 

        /// <summary>

        /// Show Print Preview 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void showPrintPreview_Click(object sender, RoutedEventArgs e)

        {

            this.tableView.ShowPrintPreviewDialog(this);

        }

 

        #endregion

    }

}

 

 

CollectionConverterDecorator.cs

 

 

using System;

using System.Collections;

using System.ComponentModel;

using System.Windows;

using System.Windows.Controls;

 

using DevExpress.Xpf.Core.Native;

using DevExpress.Xpf.Grid;

 

namespace TestProject

{

    /// <summary>

    /// 컬렉션 변환자 장식자

    /// </summary>

    public class CollectionConverterDecorator : Border, IWeakEventListener

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region Field

 

        /// <summary>

        /// SUMMARY 텍스트 속성

        /// </summary>

        public static readonly DependencyProperty SummaryTextProperty = DependencyProperty.Register

        (

            "SummaryText",

            typeof(string),

            typeof(CollectionConverterDecorator),

            new PropertyMetadata(null)

        );

 

        /// <summary>

        /// 컬렉션 속성

        /// </summary>

        public static readonly DependencyProperty CollectionProperty = DependencyProperty.Register

        (

            "Collection",

            typeof(IList),

            typeof(CollectionConverterDecorator),

            new PropertyMetadata(null, OnCollectionPropertyChanged)

        );

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Instance

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region Field

 

        /// <summary>

        /// 바인딩 리스트

        /// </summary>

        private IBindingList bindingList;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region SUMMARY 텍스트 - SummaryText

 

        /// <summary>

        /// SUMMARY 텍스트

        /// </summary>

        public string SummaryText

        {

            get

            {

                return (string)GetValue(SummaryTextProperty);

            }

            set

            {

                SetValue(SummaryTextProperty, value);

            }

        }

 

        #endregion

        #region 컬렉션 - Collection

 

        /// <summary>

        /// 컬렉션

        /// </summary>

        public IList Collection

        {

            get

            {

                return (IList)GetValue(CollectionProperty);

            }

            set

            {

                SetValue(CollectionProperty, value);

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

        #region 컬렉션 속성 변경시 처리하기 - OnCollectionPropertyChanged(pDependencyObject, e)

 

        /// <summary>

        /// 컬렉션 속성 변경시 처리하기

        /// </summary>

        /// <param name="dependencyObject">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private static void OnCollectionPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)

        {

            ((CollectionConverterDecorator)dependencyObject).OnCollectionPropertyChanged();

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Instance

        //////////////////////////////////////////////////////////////////////////////// Public

 

        // IWeakEventListener

        #region 약한 이벤트 수신하기 - ReceiveWeakEvent(managerType, sender, e)

 

        /// <summary>

        /// 약한 이벤트 수신하기

        /// </summary>

        /// <param name="managerType">관리자 타입</param>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        /// <returns>처리 결과</returns>

        public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)

        {

            if(managerType == typeof(ListChangedEventManager))

            {

                InvalidateMeasure();

 

                return true;

            }

 

            return false;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 측정하기 (오버라이드) - MeasureOverride(availableSize)

 

        /// <summary>

        /// 측정하기 (오버라이드)

        /// </summary>

        /// <param name="availableSize">이용 가능한 크기</param>

        /// <returns>측정 크기</returns>

        protected override Size MeasureOverride(Size availableSize)

        {

            if(this.bindingList != null)

            {

                SummaryText = GetSummaryText();

            }

 

            return base.MeasureOverride(availableSize);

        }

 

        #endregion

 

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

 

        #region 컬렉션 속성 변경시 처리하기 - OnCollectionPropertyChanged()

 

        /// <summary>

        /// 컬렉션 속성 변경시 처리하기

        /// </summary>

        private void OnCollectionPropertyChanged()

        {

            if(this.bindingList != null)

            {

                ListChangedEventManager.RemoveListener(this.bindingList, this);

            }

 

            this.bindingList = BindingListAdapter.CreateFromList(Collection, ItemPropertyNotificationMode.All);

 

            if(this.bindingList != null)

            {

                ListChangedEventManager.AddListener(this.bindingList, this);

            }

        }

 

        #endregion

        #region SUMMARY 텍스트 구하기 - GetSummaryText()

 

        /// <summary>

        /// SUMMARY 텍스트 구하기

        /// </summary>

        /// <returns>요약 텍스트</returns>

        private string GetSummaryText()

        {

            string summaryText = string.Empty;

 

            foreach(GridGroupSummaryData gridGroupSummaryData in this.bindingList)

            {

                summaryText += gridGroupSummaryData.Text;

 

                if(!gridGroupSummaryData.IsLast)

                {

                    summaryText += ", ";

                }

            }

 

            return summaryText;

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요