■ ScrollViewer 클래스의 ScrollChanged 이벤트를 사용해 그리드 컨트롤을 흉내내는 방법을 보여준다.
▶ 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 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="ScrollViewer 클래스 : ScrollChanged 이벤트를 사용해 그리드 컨트롤 흉내내기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ScrollViewer Name="headerScrollViewer" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"> <Grid Background="Beige"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" SharedSizeGroup="column1" /> <ColumnDefinition Width="100" SharedSizeGroup="column2" /> <ColumnDefinition Width="100" SharedSizeGroup="column3" /> <ColumnDefinition Width="100" SharedSizeGroup="column4" /> <ColumnDefinition Width="100" SharedSizeGroup="column5" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="25" /> </Grid.RowDefinitions> <Border Grid.Column="0" BorderThickness="1" BorderBrush="DarkGray"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black">헤더 1 </TextBlock> </Border> <Border Grid.Column="1" BorderThickness="1" BorderBrush="DarkGray"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black">헤더 2 </TextBlock> </Border> <Border Grid.Column="2" BorderThickness="1" BorderBrush="DarkGray"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black">헤더 3 </TextBlock> </Border> <Border Grid.Column="3" BorderThickness="1" BorderBrush="DarkGray"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black">헤더 4 </TextBlock> </Border> <Border Grid.Column="4" BorderThickness="1" BorderBrush="DarkGray"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black">헤더 5 </TextBlock> </Border> <FrameworkElement Grid.Column="5" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" /> </Grid> </ScrollViewer> <ScrollViewer Name="contentScrollViewer" Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="AUto"> <Grid Name="contentGrid" Background="White"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" SharedSizeGroup="column1" /> <ColumnDefinition Width="100" SharedSizeGroup="column2" /> <ColumnDefinition Width="100" SharedSizeGroup="column3" /> <ColumnDefinition Width="100" SharedSizeGroup="column4" /> <ColumnDefinition Width="100" SharedSizeGroup="column5" /> </Grid.ColumnDefinitions> </Grid> </ScrollViewer> </Grid> </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 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 |
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.Loaded += Window_Loaded; this.contentScrollViewer.ScrollChanged += contentScrollViewer_ScrollChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { for(int y = 0; y < 1000; y++) { RowDefinition rowDefinition = new RowDefinition(); rowDefinition.Height = new GridLength(25d, GridUnitType.Pixel); this.contentGrid.RowDefinitions.Add(rowDefinition); } for(int y = 0; y < 1000; y++) { for(int x = 0; x < 5; x++) { TextBlock textBlock = new TextBlock(); textBlock.Margin = new Thickness(3); textBlock.Foreground = Brushes.Black; textBlock.Text = string.Format("셀({0},{1})", y, x); Border border = new Border(); border.BorderThickness = new Thickness(0.5); border.BorderBrush = Brushes.DarkGray; border.Child = textBlock; Grid.SetRow (border, y); Grid.SetColumn(border, x); this.contentGrid.Children.Add(border); } } } #endregion #region 컨텐트 스크롤 뷰어 스크롤 변경시 처리하기 - contentScrollViewer_ScrollChanged(sender, e) /// <summary> /// 컨텐트 스크롤 뷰어 스크롤 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void contentScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) { this.headerScrollViewer.ScrollToHorizontalOffset(this.contentScrollViewer.ContentHorizontalOffset); } #endregion } } |