■ ScrollViewer 엘리먼트를 사용하는 기본적인 방법을 보여준다.
▶ MainPage.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 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 |
<Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="50" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <ScrollViewer Name="scrollViewer" Grid.Column="0" IsTabStop="True" ZoomMode="Disabled" IsVerticalScrollChainingEnabled="True" ManipulationCompleted="scrollViewer_ManipulationCompleted"> <Image HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="None" Source="ms-appx:///IMAGE/cliff.jpg" /> </ScrollViewer> <Grid MinWidth="200" Grid.Column="2"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="0 0 10 0" Text="확대/축소 모드" /> <ComboBox Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" SelectionChanged="zoomModeComboBox_SelectionChanged"> <ComboBoxItem>Enabled</ComboBoxItem> <ComboBoxItem IsSelected="True">Disabled</ComboBoxItem> </ComboBox> <Slider Name="zoomSlider" Grid.Row="1" Grid.ColumnSpan="2" Margin="0 10 0 0" Header="확대/축소" IsEnabled="False" Minimum="{x:Bind scrollViewer.MinZoomFactor, Mode=OneWay}" Maximum="{x:Bind scrollViewer.MaxZoomFactor, Mode=OneWay}" Value="1" ValueChanged="zoomSlider_ValueChanged" /> <TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="0 10" Text="스크롤 모드" /> <TextBlock Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" Margin="0 0 10 0" Text="수평" /> <ComboBox Grid.Row="3" Grid.Column="1" HorizontalAlignment="Stretch" SelectedIndex="1" SelectionChanged="horizontalScrollModeComboBox_SelectionChanged"> <ComboBoxItem>Auto</ComboBoxItem> <ComboBoxItem>Enabled</ComboBoxItem> <ComboBoxItem>Disabled</ComboBoxItem> </ComboBox> <TextBlock Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" Margin="0 10 10 0" Text="수직" /> <ComboBox Grid.Row="4" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0 10 0 0" SelectedIndex="1" SelectionChanged="verticalScrollModeComboBox_SelectionChanged"> <ComboBoxItem>Auto</ComboBoxItem> <ComboBoxItem>Enabled</ComboBoxItem> <ComboBoxItem>Disabled</ComboBoxItem> </ComboBox> <TextBlock Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="0 20 0 10" Text="스크롤바 표시" /> <TextBlock Grid.Row="6" Grid.Column="0" VerticalAlignment="Center" Margin="0 0 10 0" Text="수평" /> <ComboBox Grid.Row="6" Grid.Column="1" HorizontalAlignment="Stretch" SelectedIndex="3" SelectionChanged="horizontalScrollBarVisibilityComboBox_SelectionChanged"> <ComboBoxItem>Auto</ComboBoxItem> <ComboBoxItem>Visible</ComboBoxItem> <ComboBoxItem>Hidden</ComboBoxItem> <ComboBoxItem>Disabled</ComboBoxItem> </ComboBox> <TextBlock Grid.Row="7" Grid.Column="0" VerticalAlignment="Center" Margin="0 10 10 0" Text="수직" /> <ComboBox Grid.Row="7" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0 10 0 0" SelectedIndex="1" SelectionChanged="verticalScrollBarVisibilityComboBox_SelectionChanged"> <ComboBoxItem>Auto</ComboBoxItem> <ComboBoxItem>Visible</ComboBoxItem> <ComboBoxItem>Hidden</ComboBoxItem> <ComboBoxItem>Disabled</ComboBoxItem> </ComboBox> </Grid> </Grid> </Page> |
▶ MainPage.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 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
using Windows.Foundation; using Windows.Graphics.Display; using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Input; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); #region 윈도우 크기를 설정한다. double width = 800d; double height = 600d; double dpi = (double)DisplayInformation.GetForCurrentView().LogicalDpi; ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; Size windowSize = new Size(width * 96d / dpi, height * 96d / dpi); ApplicationView.PreferredLaunchViewSize = windowSize; Window.Current.Activate(); ApplicationView.GetForCurrentView().TryResizeView(windowSize); #endregion #region 윈도우 제목을 설정한다. ApplicationView.GetForCurrentView().Title = "ScrollViewer 엘리먼트 사용하기"; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 스크롤 뷰어 조작 완료시 처리하기 - scrollViewer_ManipulationCompleted(sender, e) /// <summary> /// 스크롤 뷰어 조작 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scrollViewer_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e) { this.zoomSlider.Value = this.scrollViewer.ZoomFactor; } #endregion #region 확대/축소 모드 콤보 박스 선택 변경시 처리하기 - zoomModeComboBox_SelectionChanged(sender, e) /// <summary> /// 확대/축소 모드 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void zoomModeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(this.scrollViewer != null && this.zoomSlider != null) { if(sender is ComboBox comboBox) { switch(comboBox.SelectedIndex) { case 0 : // Enabled this.scrollViewer.ZoomMode = ZoomMode.Enabled; this.zoomSlider.IsEnabled = true; break; case 1 : // Disabled default : // Disabled this.scrollViewer.ZoomMode = ZoomMode.Disabled; this.scrollViewer.ChangeView(null, null, (float)1.0); this.zoomSlider.Value = 1; this.zoomSlider.IsEnabled = false; break; } } } } #endregion #region 확대/축소 슬라이더 값 변경시 처리하기 - zoomSlider_ValueChanged(sender, e) /// <summary> /// 확대/축소 슬라이더 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void zoomSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if(this.scrollViewer != null) { this.scrollViewer.ChangeView(null, null, (float)e.NewValue); } } #endregion #region 수평 스크롤 모드 콤보 박스 선택 변경시 처리하기 - horizontalScrollModeComboBox_SelectionChanged(sender, e) /// <summary> /// 수평 스크롤 모드 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void horizontalScrollModeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(this.scrollViewer != null) { if(sender is ComboBox comboBox) { switch(comboBox.SelectedIndex) { case 0 : this.scrollViewer.HorizontalScrollMode = ScrollMode.Auto; break; case 1 : this.scrollViewer.HorizontalScrollMode = ScrollMode.Enabled; break; case 2 : this.scrollViewer.HorizontalScrollMode = ScrollMode.Disabled; break; default : this.scrollViewer.HorizontalScrollMode = ScrollMode.Enabled; break; } } } } #endregion #region 수직 스크롤 모드 콤보 박스 선택 변경시 처리하기 - verticalScrollModeComboBox_SelectionChanged(sender, e) /// <summary> /// 수직 스크롤 모드 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e"></param> private void verticalScrollModeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(this.scrollViewer != null) { if(sender is ComboBox comboBox) { switch(comboBox.SelectedIndex) { case 0 : this.scrollViewer.VerticalScrollMode = ScrollMode.Auto; break; case 1 : this.scrollViewer.VerticalScrollMode = ScrollMode.Enabled; break; case 2 : this.scrollViewer.VerticalScrollMode = ScrollMode.Disabled; break; default : this.scrollViewer.VerticalScrollMode = ScrollMode.Enabled; break; } } } } #endregion #region 수평 스크롤바 표시 콤보 박스 선택 변경시 처리하기 - horizontalScrollBarVisibilityComboBox_SelectionChanged(sender, e) /// <summary> /// 수평 스크롤바 표시 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void horizontalScrollBarVisibilityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(this.scrollViewer != null) { if(sender is ComboBox comboBox) { switch(comboBox.SelectedIndex) { case 0 : this.scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; break; case 1 : this.scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible; break; case 2 : this.scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden; break; case 3 : this.scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled; break; default : this.scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled; break; } } } } #endregion #region 수직 스크롤바 표시 콤보 박스 선택 변경시 처리하기 - verticalScrollBarVisibilityComboBox_SelectionChanged(sender, e) /// <summary> /// 수직 스크롤바 표시 콤보 박스 선택 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void verticalScrollBarVisibilityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(this.scrollViewer != null) { if(sender is ComboBox comboBox) { switch(comboBox.SelectedIndex) { case 0 : this.scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; break; case 1 : this.scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible; break; case 2 : this.scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden; break; case 3 : this.scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled; break; default : this.scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible; break; } } } } #endregion } } |