■ Grid 클래스에서 셀 패딩 속성을 추가하는 방법을 보여준다.
▶ MarginUIElementCollection .cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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 } } |