■ 움직이는 랩 패널(Wrap Panel)을 사용하는 방법을 보여준다.
▶ AnimatedWrapPanel.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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; using System.Windows.Media.Animation; namespace TestProject { /// <summary> /// 움직이는 랩 패널 /// </summary> public class AnimatedWrapPanel : Panel, IScrollInfo { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 무한 크기 /// </summary> private static Size _infiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity); #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 라인 크기 /// </summary> private const double LINE_SIZE = 16; /// <summary> /// 휠 크기 /// </summary> private const double WHEEL_SIZE = 3 * LINE_SIZE; /// <summary> /// 수평 스크롤 가능 여부 /// </summary> private bool canHorizontallyScroll; /// <summary> /// 수직 스크롤 가능 여부 /// </summary> private bool canVerticallyScroll; /// <summary> /// 스크롤 소유자 /// </summary> private ScrollViewer scrollOwner; /// <summary> /// 오프셋 벡터 /// </summary> private Vector offsetVector; /// <summary> /// 범위 크기 /// </summary> private Size extentSize; /// <summary> /// 뷰포트 크기 /// </summary> private Size viewportSize; /// <summary> /// 애니메이션 시간 범위 /// </summary> private TimeSpan animationTimeSpan = TimeSpan.FromMilliseconds(200); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 스크롤 소유자 - ScrollOwner /// <summary> /// 스크롤 소유자 /// </summary> public ScrollViewer ScrollOwner { get { return this.scrollOwner; } set { this.scrollOwner = value; } } #endregion #region 수평 스크롤 가능 여부 - CanHorizontallyScroll /// <summary> /// 수평 스크롤 가능 여부 /// </summary> public bool CanHorizontallyScroll { get { return this.canHorizontallyScroll; } set { this.canHorizontallyScroll = value; } } #endregion #region 수직 스크롤 가능 여부 - CanVerticallyScroll /// <summary> /// 수직 스크롤 가능 여부 /// </summary> public bool CanVerticallyScroll { get { return this.canVerticallyScroll; } set { this.canVerticallyScroll = value; } } #endregion #region 범위 너비 - ExtentWidth /// <summary> /// 범위 너비 /// </summary> public double ExtentWidth { get { return this.extentSize.Width; } } #endregion #region 범위 높이 - ExtentHeight /// <summary> /// 범위 높이 /// </summary> public double ExtentHeight { get { return this.extentSize.Height; } } #endregion #region 수평 오프셋 - HorizontalOffset /// <summary> /// 수평 오프셋 /// </summary> public double HorizontalOffset { get { return this.offsetVector.X; } } #endregion #region 수직 오프셋 - VerticalOffset /// <summary> /// 수직 오프셋 /// </summary> public double VerticalOffset { get { return this.offsetVector.Y; } } #endregion #region 뷰포트 너비 - ViewportWidth /// <summary> /// 뷰포트 너비 /// </summary> public double ViewportWidth { get { return this.viewportSize.Width; } } #endregion #region 뷰포트 높이 - ViewportHeight /// <summary> /// 뷰포트 높이 /// </summary> public double ViewportHeight { get { return this.viewportSize.Height; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 한 줄 왼쪽으로 이동하기 - LineLeft() /// <summary> /// 한 줄 왼쪽으로 이동하기 /// </summary> public void LineLeft() { SetHorizontalOffset(HorizontalOffset - LINE_SIZE); } #endregion #region 한 줄 위로 이동하기 - LineUp() /// <summary> /// 한 줄 위로 이동하기 /// </summary> public void LineUp() { SetVerticalOffset(VerticalOffset - LINE_SIZE); } #endregion #region 한 줄 오른쪽으로 이동하기 - LineRight() /// <summary> /// 한 줄 오른쪽으로 이동하기 /// </summary> public void LineRight() { SetHorizontalOffset(HorizontalOffset + LINE_SIZE); } #endregion #region 한 줄 아래로 이동하기 - LineDown() /// <summary> /// 한 줄 아래로 이동하기 /// </summary> public void LineDown() { SetVerticalOffset(VerticalOffset + LINE_SIZE); } #endregion #region 마우스 휠 왼쪽으로 이동하기 - MouseWheelLeft() /// <summary> /// 마우스 휠 왼쪽으로 이동하기 /// </summary> public void MouseWheelLeft() { SetHorizontalOffset(HorizontalOffset - WHEEL_SIZE); } #endregion #region 마우스 휠 위로 이동하기 - MouseWheelUp() /// <summary> /// 마우스 휠 위로 이동하기 /// </summary> public void MouseWheelUp() { SetVerticalOffset(VerticalOffset - WHEEL_SIZE); } #endregion #region 마우스 휠 오른쪽으로 이동하기 - MouseWheelRight() /// <summary> /// 마우스 휠 오른쪽으로 이동하기 /// </summary> public void MouseWheelRight() { SetHorizontalOffset(HorizontalOffset + WHEEL_SIZE); } #endregion #region 마우스 휠 아래로 이동하기 - MouseWheelDown() /// <summary> /// 마우스 휠 아래로 이동하기 /// </summary> public void MouseWheelDown() { SetVerticalOffset(VerticalOffset + WHEEL_SIZE); } #endregion #region 페이지 왼쪽으로 이동하기 - PageLeft() /// <summary> /// 페이지 왼쪽으로 이동하기 /// </summary> public void PageLeft() { SetHorizontalOffset(HorizontalOffset - ViewportWidth); } #endregion #region 페이지 위로 이동하기 - PageUp() /// <summary> /// 페이지 위로 이동하기 /// </summary> public void PageUp() { SetVerticalOffset(VerticalOffset - ViewportHeight); } #endregion #region 페이지 오른쪽으로 이동하기 - PageRight() /// <summary> /// 페이지 오른쪽으로 이동하기 /// </summary> public void PageRight() { SetHorizontalOffset(HorizontalOffset + ViewportWidth); } #endregion #region 페이지 아래로 이동하기 - PageDown() /// <summary> /// 페이지 아래로 이동하기 /// </summary> public void PageDown() { SetVerticalOffset(VerticalOffset + ViewportHeight); } #endregion #region 표시하기 - MakeVisible(visual, rectangle) /// <summary> /// 표시하기 /// </summary> /// <param name="visual">비주얼 객체</param> /// <param name="rectangle">사각형</param> /// <returns>사각형</returns> public Rect MakeVisible(Visual visual, Rect rectangle) { if(rectangle.IsEmpty || visual == null || visual == this || !base.IsAncestorOf(visual)) { return Rect.Empty; } rectangle = visual.TransformToAncestor(this).TransformBounds(rectangle); Rect viewRect = new Rect(HorizontalOffset, VerticalOffset, ViewportWidth, ViewportHeight); rectangle.X += viewRect.X; rectangle.Y += viewRect.Y; viewRect.X = CalculateNewScrollOffset(viewRect.Left, viewRect.Right , rectangle.Left, rectangle.Right ); viewRect.Y = CalculateNewScrollOffset(viewRect.Top , viewRect.Bottom, rectangle.Top , rectangle.Bottom); SetHorizontalOffset(viewRect.X); SetVerticalOffset(viewRect.Y); rectangle.Intersect(viewRect); rectangle.X -= viewRect.X; rectangle.Y -= viewRect.Y; return rectangle; } #endregion #region 수평 오프셋 설정하기 - SetHorizontalOffset(offset) /// <summary> /// 수평 오프셋 설정하기 /// </summary> /// <param name="offset">오프셋</param> public void SetHorizontalOffset(double offset) { offset = Math.Max(0, Math.Min(offset, ExtentWidth - ViewportWidth)); if(offset != this.offsetVector.Y) { this.offsetVector.X = offset; InvalidateArrange(); } } #endregion #region 수직 오프셋 설정하기 - SetVerticalOffset(offset) /// <summary> /// 수직 오프셋 설정하기 /// </summary> /// <param name="offset">오프셋</param> public void SetVerticalOffset(double offset) { offset = Math.Max(0, Math.Min(offset, ExtentHeight - ViewportHeight)); if(offset != this.offsetVector.Y) { this.offsetVector.Y = offset; InvalidateArrange(); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 측정하기 (오버라이드) - MeasureOverride(availableSize) /// <summary> /// 측정하기 (오버라이드) /// </summary> /// <param name="availableSize">이용 가능한 크기</param> /// <returns>측정 크기</returns> protected override Size MeasureOverride(Size availableSize) { double currentX = 0; double currentY = 0; double currentLineHeight = 0; double maximumLineWidth = 0; foreach(UIElement child in Children) { child.Measure(_infiniteSize); if(currentX + child.DesiredSize.Width > availableSize.Width) { currentY += currentLineHeight; currentX = 0; currentLineHeight = 0; } currentX += child.DesiredSize.Width; if(child.DesiredSize.Height > currentLineHeight) { currentLineHeight = child.DesiredSize.Height; } if(currentX > maximumLineWidth) { maximumLineWidth = currentX; } } currentY += currentLineHeight; VerifyScrollData(availableSize, new Size(maximumLineWidth, currentY)); return this.viewportSize; } #endregion #region 배치하기 (오버라이드) - ArrangeOverride(finalSize) /// <summary> /// 배치하기 (오버라이드) /// </summary> /// <param name="finalSize">최종 크기</param> /// <returns>최종 크기</returns> protected override Size ArrangeOverride(Size finalSize) { if(Children == null || Children.Count == 0) { return finalSize; } TranslateTransform transform = null; double currentX = 0; double currentY = 0; double currentLineHeight = 0; double maximumLineWidth = 0; foreach(UIElement child in Children) { transform = child.RenderTransform as TranslateTransform; if(transform == null) { child.RenderTransformOrigin = new Point(0, 0); transform = new TranslateTransform(); child.RenderTransform = transform; } if(currentX + child.DesiredSize.Width > finalSize.Width) { currentY += currentLineHeight; currentX = 0; currentLineHeight = 0; } child.Arrange(new Rect(0, 0, child.DesiredSize.Width, child.DesiredSize.Height)); transform.BeginAnimation ( TranslateTransform.XProperty, new DoubleAnimation(currentX - HorizontalOffset, this.animationTimeSpan), HandoffBehavior.Compose ); transform.BeginAnimation ( TranslateTransform.YProperty, new DoubleAnimation(currentY - VerticalOffset, this.animationTimeSpan), HandoffBehavior.Compose ); currentX += child.DesiredSize.Width; if(child.DesiredSize.Height > currentLineHeight) { currentLineHeight = child.DesiredSize.Height; } if(currentX > maximumLineWidth) { maximumLineWidth = currentX; } } currentY += currentLineHeight; VerifyScrollData(finalSize, new Size(maximumLineWidth, currentY)); return finalSize; } #endregion #region 스크롤 데이터 검증하기 - VerifyScrollData(viewportSize, extentSize) /// <summary> /// 스크롤 데이터 검증하기 /// </summary> /// <param name="viewportSize">뷰포트 크기</param> /// <param name="extentSize">범위 크기</param> protected void VerifyScrollData(Size viewportSize, Size extentSize) { if(double.IsInfinity(viewportSize.Width)) { viewportSize.Width = extentSize.Width; } if(double.IsInfinity(viewportSize.Height)) { viewportSize.Height = extentSize.Height; } this.extentSize = extentSize; this.viewportSize = viewportSize; this.offsetVector.X = Math.Max(0, Math.Min(this.offsetVector.X, ExtentWidth - ViewportWidth )); this.offsetVector.Y = Math.Max(0, Math.Min(this.offsetVector.Y, ExtentHeight - ViewportHeight)); if(ScrollOwner != null) { ScrollOwner.InvalidateScrollInfo(); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 신규 스크롤 오프셋 계산하기 - CalculateNewScrollOffset(topView, bottomView, topChild, bottomChild) /// <summary> /// 신규 스크롤 오프셋 계산하기 /// </summary> /// <param name="topView">위쪽 뷰</param> /// <param name="bottomView">아래쪽 뷰</param> /// <param name="topChild">위쪽 자식</param> /// <param name="bottomChild">아래쪽 자식</param> /// <returns>신규 스크롤 오프셋</returns> private static double CalculateNewScrollOffset(double topView, double bottomView, double topChild, double bottomChild) { bool offsetBottom = topChild < topView && bottomChild < bottomView; bool offsetTop = bottomChild > bottomView && topChild > topView; bool tooLarge = (bottomChild - topChild) > (bottomView - topView); if(!offsetBottom && !offsetTop) { return topView; } if((offsetBottom && !tooLarge) || (offsetTop && tooLarge)) { return topChild; } return (bottomChild - (bottomView - topView)); } #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 55 56 57 58 59 60 61 62 63 64 |
<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" Width="800" Height="600" Title="움직이는 랩 패널(Wrap Panel) 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True"> <local:AnimatedWrapPanel> <Image Margin="5" Width="200" Stretch="Uniform" Source="Image\Aquarium.jpg" /> <Image Margin="5" Width="100" Stretch="Uniform" Source="Image\Ascent.jpg" /> <Image Margin="5" Width="300" Stretch="Uniform" Source="Image\Autumn.jpg" /> <Image Margin="5" Width="150" Stretch="Uniform" Source="Image\Crystal.jpg" /> <Image Margin="5" Width="250" Stretch="Uniform" Source="Image\DaVinci.jpg" /> <Image Margin="5" Width="200" Stretch="Uniform" Source="Image\Follow.jpg" /> <Image Margin="5" Width="100" Stretch="Uniform" Source="Image\Friend.jpg" /> <Image Margin="5" Width="300" Stretch="Uniform" Source="Image\Home.jpg" /> <Image Margin="5" Width="100" Stretch="Uniform" Source="Image\MoonFlower.jpg" /> </local:AnimatedWrapPanel> </ScrollViewer> </Window> |