■ Grid 클래스 : 셀 패딩 속성 추가하기

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


TestProject.zip


MarginUIElementCollection .cs

 

 

using System.Windows;

using System.Windows.Controls;

 

namespace TestProject

{

    /// <summary>

    /// 마진 UI 엘리먼트 컬렉션

    /// </summary>

    public class MarginUIElementCollection : UIElementCollection

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 커스텀 그리드

        /// </summary>

        private CellPaddingGrid customGrid;

 

        #endregion

 

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

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

 

        #region 생성자 - MarginUIElementCollection(logicalParent)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="logicalParent">논리적 부모</param>

        public MarginUIElementCollection(FrameworkElement logicalParent) : base(logicalParent,null)

        {

            this.customGrid = logicalParent as CellPaddingGrid;

        }

 

        #endregion

 

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

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

 

        #region 추가하기 - Add(element)

 

        /// <summary>

        /// 추가하기

        /// </summary>

        /// <param name="element">UI 엘리먼트</param>

        /// <returns>추가 인덱스</returns>

        public override int Add(UIElement element)

        {

            CellPaddingGrid.ApplyMargin(this.customGrid, element);

 

            return base.Add(element);

        }

 

        #endregion

        #region 삽입하기 - Insert(index, element)

 

        /// <summary>

        /// 삽입하기

        /// </summary>

        /// <param name="index">인덱스</param>

        /// <param name="element">UI 엘리먼트</param>

        public override void Insert(int index, UIElement element)

        {

            CellPaddingGrid.ApplyMargin(this.customGrid, element);

 

            base.Insert(index, element);

        }

 

        #endregion

    }

}

 

 

CellPaddingGrid.cs

 

 

using System.Windows;

using System.Windows.Controls;

 

namespace TestProject

{

    /// <summary>

    /// 셀 패딩 그리드

    /// </summary>

    public class CellPaddingGrid : Grid

    {

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

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

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

 

        #region Field

 

        /// <summary>

        /// 셀 패딩 속성

        /// </summary>

        public static readonly DependencyProperty CellPaddingProperty = DependencyProperty.Register

        (

            "CellPadding",

            typeof(Thickness),

            typeof(CellPaddingGrid),

            new FrameworkPropertyMetadata

            (

                new Thickness(0.0),

                FrameworkPropertyMetadataOptions.AffectsArrange,

                CellPaddingPropertyChangedCallback

            )

        );

 

        #endregion

 

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

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

 

        #region 셀 패딩 - CellPadding

 

        /// <summary>

        /// 셀 패딩

        /// </summary>

        public Thickness CellPadding

        {

            get

            {

                return (Thickness)GetValue(CellPaddingProperty);

            }

            set

            {

                SetValue(CellPaddingProperty, value);

            }

        }

 

        #endregion

 

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

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

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

 

        #region 마진 적용하기 - ApplyMargin(grid, element)

 

        /// <summary>

        /// 마진 적용하기

        /// </summary>

        /// <param name="grid">그리드</param>

        /// <param name="element">UI 엘리먼트</param>

        public static void ApplyMargin(CellPaddingGrid grid, UIElement element)

        {

            FrameworkElement frameworkElement = element as FrameworkElement;

 

            Thickness cellPadding = grid.CellPadding;

 

            CellPaddingGrid grid2 = element as CellPaddingGrid;

 

            if(grid2 != null)

            {

                grid2.CellPadding = cellPadding;

            }

            else

            {

                frameworkElement.Margin = cellPadding;

            }

        }

 

        #endregion

 

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

 

        #region 셀 패딩 속성 변경시 콜백 처리하기 - CellPaddingPropertyChangedCallback(d, e)

 

        /// <summary>

        /// 셀 패딩 속성 변경시 콜백 처리하기

        /// </summary>

        /// <param name="d">의존 객체</param>

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

        private static void CellPaddingPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            CellPaddingGrid grid = d as CellPaddingGrid;

 

            foreach(UIElement element in grid.Children)

            {

                ApplyMargin(grid, element);

            }

        }

 

        #endregion

 

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

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

 

        #region 비주얼 자식 변경시 처리하기 - OnVisualChildrenChanged(addVisual, removeVisual)

 

        /// <summary>

        /// 비주얼 자식 변경시 처리하기

        /// </summary>

        /// <param name="addVisual">추가 비주얼</param>

        /// <param name="removeVisual">제거 비주얼</param>

        protected override void OnVisualChildrenChanged(DependencyObject addVisual, DependencyObject removeVisual)

        {

            FrameworkElement childElement = addVisual as FrameworkElement;

 

            ApplyMargin(this, childElement);

 

            base.OnVisualChildrenChanged(addVisual, removeVisual);

        }

 

        #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:local="clr-namespace:TestProject"

    Title="Grid 클래스 : 셀 패딩 속성 추가하기"

    Width="800"

    Height="600"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <local:CellPaddingGrid x:Name="grid" Margin="10" CellPadding="3">

        <Grid.RowDefinitions>

            <RowDefinition />

            <RowDefinition />

            <RowDefinition />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition />

            <ColumnDefinition />

            <ColumnDefinition />

        </Grid.ColumnDefinitions>

        <Button Name="button1" Grid.Row="0" Grid.Column="0"

            Content="셀 패딩 == 10" />

        <Button Name="button2" Grid.Row="0" Grid.Column="1"

            Content="셀 패딩 == 3" />

        <Button Grid.Row="0" Grid.Column="2"

            Content="버튼 3" />

        <Button Grid.Row="1" Grid.ColumnSpan="2"

            Content="버튼 4" />

        <Button Grid.Row="1" Grid.Column="2" Grid.RowSpan="2"

            Content="버튼 5" />

        <Button Grid.Row="2" Grid.Column="0"

            Content="버튼 6" />

        <local:CellPaddingGrid Grid.Row="2" Grid.Column="1">

            <Grid.ColumnDefinitions>

                <ColumnDefinition />

                <ColumnDefinition />

            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>

                <RowDefinition />

                <RowDefinition />

            </Grid.RowDefinitions>

            <Button Grid.Row="0" Grid.Column="0"

                Content="버튼 7" />

            <Button Grid.Row="0" Grid.Column="1"

                Content="버튼 8" />

            <Button Grid.Row="1" Grid.Column="0"

                Content="버튼 9" />

            <Button Grid.Row="1" Grid.Column="1"

                Content="버튼 10" />

        </local:CellPaddingGrid>

    </local:CellPaddingGrid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            button1.Click += button1_Click;

            button2.Click += button2_Click;

        }

 

        #endregion

 

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

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

 

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

 

        /// <summary>

        /// 버튼 1 클릭시 처리하기

        /// </summary>

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

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

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            this.grid.CellPadding = new Thickness(10.0);

        }

 

        #endregion

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

 

        /// <summary>

        /// 버튼 2 클릭시 처리하기

        /// </summary>

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

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

        private void button2_Click(object sender, RoutedEventArgs e)

        {

            this.grid.CellPadding = new Thickness(3.0);

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요