■ 폴더 컨테이너를 사용하는 방법을 보여준다.
▶ FolderInfo.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 |
using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; namespace TestProject { /// <summary> /// 폴더 정보 /// </summary> public class FolderInfo : DependencyObject { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 키 의존 속성 /// </summary> public static readonly DependencyProperty KeyProperty; /// <summary> /// 버튼 의존 속성 /// </summary> public static readonly DependencyProperty ButtonProperty; /// <summary> /// 헤더 텍스트 의존 속성 /// </summary> public static readonly DependencyProperty HeaderTextProperty; /// <summary> /// 행 정의 의존 속성 /// </summary> public static readonly DependencyProperty RowDefinitionProperty; /// <summary> /// 행 높이 의존 속성 /// </summary> public static readonly DependencyProperty RowHeightProperty; /// <summary> /// 실제 행 높이 의존 속성 /// </summary> public static readonly DependencyProperty ActualRowHeightProperty; /// <summary> /// 그리드 스플리터 /// </summary> public static readonly DependencyProperty GridSplitterProperty; /// <summary> /// 폴더 의존 속성 /// </summary> public static readonly DependencyProperty FolderProperty; /// <summary> /// 폴더 자식 의존 속성 /// </summary> public static readonly DependencyProperty FolderChildProperty; /// <summary> /// 표시 여부 의존 속성 /// </summary> public static readonly DependencyProperty VisibleProperty; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - FolderInfo() /// <summary> /// 생성자 /// </summary> static FolderInfo() { KeyProperty = UIHelper.RegisterDependencyProperty ( string.Empty, false, false, false, true, null, "Key", typeof(string), typeof(FolderInfo), null ); ButtonProperty = UIHelper.RegisterDependencyProperty ( null, false, false, false, true, null, "Button", typeof(ToggleButton), typeof(FolderInfo), null ); HeaderTextProperty = UIHelper.RegisterDependencyProperty ( string.Empty, false, false, false, true, null, "HeaderText", typeof(string), typeof(FolderInfo), null ); RowDefinitionProperty = UIHelper.RegisterDependencyProperty ( null, false, false, false, true, null, "RowDefinition", typeof(RowDefinition), typeof(FolderInfo), null ); RowHeightProperty = UIHelper.RegisterDependencyProperty ( 0d, false, false, false, true, null, "RowHeight", typeof(double), typeof(FolderInfo), null ); ActualRowHeightProperty = UIHelper.RegisterDependencyProperty ( 0d, false, false, false, true, null, "ActualRowHeight", typeof(double), typeof(FolderInfo), null ); GridSplitterProperty = UIHelper.RegisterDependencyProperty ( null, false, false, false, true, null, "GridSplitter", typeof(GridSplitter), typeof(FolderInfo), null ); FolderProperty = UIHelper.RegisterDependencyProperty ( null, false, false, false, true, null, "Folder", typeof(Folder), typeof(FolderInfo), null ); FolderChildProperty = UIHelper.RegisterDependencyProperty ( null, false, false, false, true, null, "FolderChild", typeof(UIElement), typeof(FolderInfo), null ); VisibleProperty = UIHelper.RegisterDependencyProperty ( false, false, false, false, true, null, "Visible", typeof(bool), typeof(FolderInfo), null ); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 키 - Key /// <summary> /// 키 /// </summary> public string Key { get { return (string)GetValue(KeyProperty); } set { SetValue(KeyProperty, value); } } #endregion #region 버튼 - Button /// <summary> /// 버튼 /// </summary> public ToggleButton Button { get { return (ToggleButton)GetValue(ButtonProperty); } set { SetValue(ButtonProperty, value); } } #endregion #region 헤더 텍스트 - HeaderText /// <summary> /// 헤더 텍스트 /// </summary> public string HeaderText { get { return (string)GetValue(HeaderTextProperty); } set { SetValue(HeaderTextProperty, value); } } #endregion #region 행 정의 - RowDefinition /// <summary> /// 행 정의 /// </summary> public RowDefinition RowDefinition { get { return (RowDefinition)GetValue(RowDefinitionProperty); } set { SetValue(RowDefinitionProperty, value); } } #endregion #region 행 높이 - RowHeight /// <summary> /// 행 높이 /// </summary> public double RowHeight { get { return (double)GetValue(RowHeightProperty); } set { SetValue(RowHeightProperty, value); } } #endregion #region 실제 행 높이 - ActualRowHeight /// <summary> /// 실제 행 높이 /// </summary> public double ActualRowHeight { get { return (double)GetValue(ActualRowHeightProperty); } set { SetValue(ActualRowHeightProperty, value); } } #endregion #region 그리드 스플리터 - GridSplitter /// <summary> /// 그리드 스플리터 /// </summary> public GridSplitter GridSplitter { get { return (GridSplitter)GetValue(GridSplitterProperty); } set { SetValue(GridSplitterProperty, value); } } #endregion #region 폴더 - Folder /// <summary> /// 폴더 /// </summary> public Folder Folder { get { return (Folder)GetValue(FolderProperty); } set { SetValue(FolderProperty, value); } } #endregion #region 폴더 자식 - FolderChild /// <summary> /// 폴더 자식 /// </summary> public UIElement FolderChild { get { return (UIElement)GetValue(FolderChildProperty); } set { SetValue(FolderChildProperty, value); } } #endregion #region 표시 여부 - Visible /// <summary> /// 표시 여부 /// </summary> public bool Visible { get { return (bool)GetValue(VisibleProperty); } set { SetValue(VisibleProperty, value); } } #endregion } } |
▶ FolderInfoCollection.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 폴더 정보 컬렉션 /// </summary> public class FolderInfoCollection : ObservableCollection<FolderInfo> { } } |
▶ FolderInfoCollectionEventArgs.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 |
using System; namespace TestProject { /// <summary> /// 폴더 정보 컬렉션 이벤트 인자 /// </summary> public class FolderInfoCollectionEventArgs : EventArgs { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 폴더 정보 컬렉션 /// </summary> private FolderInfoCollection collection; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 폴더 정보 컬렉션 - Collection /// <summary> /// 폴더 정보 컬렉션 /// </summary> public FolderInfoCollection Collection { get { return this.collection; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - FolderInfoCollectionEventArgs(collection) /// <summary> /// 생성자 /// </summary> /// <param name="collection">폴더 정보 컬렉션</param> public FolderInfoCollectionEventArgs(FolderInfoCollection collection) { this.collection = collection; } #endregion } } |
▶ FolderInfoCollectionEventHandler.cs
1 2 3 4 5 6 7 8 9 10 11 |
namespace TestProject { /// <summary> /// 폴더 정보 컬렉션 이벤트 핸들러 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> public delegate void FolderInfoCollectionEventHandler(object sender, FolderInfoCollectionEventArgs e); } |
▶ FolderInfoEventArgs.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 |
using System; namespace TestProject { /// <summary> /// 폴더 정보 이벤트 인자 /// </summary> public class FolderInfoEventArgs : EventArgs { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 폴더 정보 /// </summary> private FolderInfo info; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 폴더 정보 - Info /// <summary> /// 폴더 정보 /// </summary> public FolderInfo Info { get { return this.info; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - FolderInfoEventArgs(info) /// <summary> /// 생성자 /// </summary> /// <param name="info">폴더 정보</param> public FolderInfoEventArgs(FolderInfo info) { this.info = info; } #endregion } } |
▶ FolderInfoEventHandler.cs
1 2 3 4 5 6 7 8 9 10 11 |
namespace TestProject { /// <summary> /// 폴더 정보 이벤트 핸들러 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> public delegate void FolderInfoEventHandler(object sender, FolderInfoEventArgs e); } |
▶ Folder.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 |
<ContentControl x:Class="TestProject.Folder" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:src="clr-namespace:TestProject" mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="200"> <Control.Template> <ControlTemplate TargetType="ContentControl"> <Border Name="rootBorder" BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PanelBorder}" BorderThickness="1"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Border Name="headerBorder" Grid.Row="0" Padding="1" Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderHeight}" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderBackground}"> <DockPanel> <src:MonoButton x:Name="closeButton" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CloseButtonVisibility}" DockPanel.Dock="Right" VerticalAlignment="Center" Margin="1" Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ButtonWidth}" Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ButtonHeight}" MonoType="Close" Regular="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderButtonRegular}" Hover="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderButtonHover}" Press="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderButtonPress}" Click="closeButton_Click" /> <src:MonoButton x:Name="expandButton" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ExpandButtonVisibility}" DockPanel.Dock="Right" VerticalAlignment="Center" Margin="1" Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ButtonWidth}" Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ButtonHeight}" MonoType="Collapse" Regular="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderButtonRegular}" Hover="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderButtonHover}" Press="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderButtonPress}" Click="expandButton_Click" /> <TextBlock Name="headerTextBlock" VerticalAlignment="Center" Margin="10 0 0 0" FontWeight="Bold" Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderForeground}" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HeaderText}" /> </DockPanel> </Border> <ContentPresenter Name="contentPresenter" Grid.Row="1" Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" /> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="src:Folder.IsExpanded" Value="False"> <Setter TargetName="expandButton" Property="MonoType" Value="Expand" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Control.Template> </ContentControl> |
▶ Folder.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 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 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 |
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace TestProject { /// <summary> /// 폴더 /// </summary> public partial class Folder : ContentControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 확장 여부 변경시 - IsExpandedChanged /// <summary> /// 확장 여부 변경시 /// </summary> public event EventHandler IsExpandedChanged; #endregion #region Expand 버튼 클릭시 - ExpandButtonClick /// <summary> /// Expand 버튼 클릭시 /// </summary> public event EventHandler ExpandButtonClick; #endregion #region Close 버튼 클릭시 - CloseButtonClick /// <summary> /// Close 버튼 클릭시 /// </summary> public event EventHandler CloseButtonClick; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 패널 테두리 의존 속성 /// </summary> public static readonly DependencyProperty PanelBorderProperty; /// <summary> /// 자동 높이 의존 속성 /// </summary> public static readonly DependencyProperty AutoHeightProperty; /// <summary> /// 헤더 높이 의존 속성 /// </summary> public static readonly DependencyProperty HeaderHeightProperty; /// <summary> /// 헤더 배경색 의존 속성 /// </summary> public static readonly DependencyProperty HeaderBackgroundProperty; /// <summary> /// 헤더 전경색 의존 속성 /// </summary> public static readonly DependencyProperty HeaderForegroundProperty; /// <summary> /// 헤더 버튼 레귤러 의존 속성 /// </summary> public static readonly DependencyProperty HeaderButtonRegularProperty; /// <summary> /// 헤더 버튼 호버 의존 속성 /// </summary> public static readonly DependencyProperty HeaderButtonHoverProperty; /// <summary> /// 버튼 프레스 의존 속성 /// </summary> public static readonly DependencyProperty HeaderButtonPressProperty; /// <summary> /// 헤더 텍스트 의존 속성 /// </summary> public static readonly DependencyProperty HeaderTextProperty; /// <summary> /// 확장 여부 의존 속성 /// </summary> public static readonly DependencyProperty IsExpandedProperty; /// <summary> /// Expand 버튼 표시 여부 의존 속성 /// </summary> public static readonly DependencyProperty ExpandButtonVisibilityProperty; /// <summary> /// Close 버튼 표시 여부 의존 속성 /// </summary> public static readonly DependencyProperty CloseButtonVisibilityProperty; /// <summary> /// 버튼 너비 의존 속성 /// </summary> public static readonly DependencyProperty ButtonWidthProperty; /// <summary> /// 버튼 높이 의존 속성 /// </summary> public static readonly DependencyProperty ButtonHeightProperty; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 원본 높이 /// </summary> private double originalHeight = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 패널 테두리 - PanelBorder /// <summary> /// 패널 테두리 /// </summary> public Brush PanelBorder { get { return (Brush)GetValue(PanelBorderProperty); } set { SetValue(PanelBorderProperty, value); } } #endregion #region 자동 높이 - AutoHeight /// <summary> /// 자동 높이 /// </summary> public bool AutoHeight { get { return (bool)GetValue(AutoHeightProperty); } set { SetValue(AutoHeightProperty, value); } } #endregion #region 헤더 높이 - HeaderHeight /// <summary> /// 헤더 높이 /// </summary> public double HeaderHeight { get { return (double)GetValue(HeaderHeightProperty); } set { SetValue(HeaderHeightProperty, value); } } #endregion #region 헤더 배경색 - HeaderBackground /// <summary> /// 헤더 배경색 /// </summary> public Brush HeaderBackground { get { return (Brush)GetValue(HeaderBackgroundProperty); } set { SetValue(HeaderBackgroundProperty, value); } } #endregion #region 헤더 전경색 - HeaderForeground /// <summary> /// 헤더 전경색 /// </summary> public Brush HeaderForeground { get { return (Brush)GetValue(HeaderForegroundProperty); } set { SetValue(HeaderForegroundProperty, value); } } #endregion #region 헤더 버튼 레귤러 - HeaderButtonRegular /// <summary> /// 헤더 버튼 레귤러 /// </summary> public Brush HeaderButtonRegular { get { return (Brush)GetValue(HeaderButtonRegularProperty); } set { SetValue(HeaderButtonRegularProperty, value); } } #endregion #region 헤더 버튼 호버 - HeaderButtonHover /// <summary> /// 헤더 버튼 호버 /// </summary> public Brush HeaderButtonHover { get { return (Brush)GetValue(HeaderButtonHoverProperty); } set { SetValue(HeaderButtonHoverProperty, value); } } #endregion #region 헤더 버튼 프레스 - HeaderButtonPress /// <summary> /// 헤더 버튼 프레스 /// </summary> public Brush HeaderButtonPress { get { return (Brush)GetValue(HeaderButtonPressProperty); } set { SetValue(HeaderButtonPressProperty, value); } } #endregion #region 헤더 텍스트 - HeaderText /// <summary> /// 헤더 텍스트 /// </summary> public string HeaderText { get { return (string)GetValue(HeaderTextProperty); } set { SetValue(HeaderTextProperty, value); } } #endregion #region 확장 여부 - IsExpanded /// <summary> /// 확장 여부 /// </summary> public bool IsExpanded { get { return (bool)GetValue(IsExpandedProperty); } set { SetValue(IsExpandedProperty, value); } } #endregion #region Expand 버튼 표시 여부 - ExpandButtonVisibility /// <summary> /// Expand 버튼 표시 여부 /// </summary> public Visibility ExpandButtonVisibility { get { return (Visibility)GetValue(ExpandButtonVisibilityProperty); } set { SetValue(ExpandButtonVisibilityProperty, value); } } #endregion #region Close 버튼 표시 여부 - CloseButtonVisibility /// <summary> /// Close 버튼 표시 여부 /// </summary> public Visibility CloseButtonVisibility { get { return (Visibility)GetValue(CloseButtonVisibilityProperty); } set { SetValue(CloseButtonVisibilityProperty, value); } } #endregion #region 버튼 너비 - ButtonWidth /// <summary> /// 버튼 너비 /// </summary> public double ButtonWidth { get { return (double)GetValue(ButtonWidthProperty); } set { SetValue(ButtonWidthProperty, value); } } #endregion #region 버튼 높이 - ButtonHeight /// <summary> /// 버튼 높이 /// </summary> public double ButtonHeight { get { return (double)GetValue(ButtonHeightProperty); } set { SetValue(ButtonHeightProperty, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - Folder() /// <summary> /// 생성자 /// </summary> static Folder() { PanelBorderProperty = UIHelper.RegisterDependencyProperty ( Brushes.RoyalBlue, false, false, false, true, null, "PanelBorder", typeof(Brush), typeof(Folder), null ); AutoHeightProperty = UIHelper.RegisterDependencyProperty ( false, false, false, false, true, null, "AutoHeight", typeof(bool), typeof(Folder), null ); HeaderHeightProperty = UIHelper.RegisterDependencyProperty ( 26d, false, false, false, true, null, "HeaderHeight", typeof(double), typeof(Folder), null ); HeaderForegroundProperty = UIHelper.RegisterDependencyProperty ( Brushes.White, false, false, false, true, null, "HeaderForeground", typeof(Brush), typeof(Folder), null ); HeaderBackgroundProperty = UIHelper.RegisterDependencyProperty ( Brushes.RoyalBlue, false, false, false, true, null, "HeaderBackground", typeof(Brush), typeof(Folder), null ); HeaderButtonRegularProperty = UIHelper.RegisterDependencyProperty ( Brushes.White, false, false, false, true, null, "HeaderButtonRegular", typeof(Brush), typeof(Folder), null ); HeaderButtonHoverProperty = UIHelper.RegisterDependencyProperty ( Brushes.Yellow, false, false, false, true, null, "HeaderButtonHover", typeof(Brush), typeof(Folder), null ); HeaderButtonPressProperty = UIHelper.RegisterDependencyProperty ( Brushes.Gold, false, false, false, true, null, "HeaderButtonPress", typeof(Brush), typeof(Folder), null ); HeaderTextProperty = UIHelper.RegisterDependencyProperty ( string.Empty, false, false, false, true, null, "HeaderText", typeof(string), typeof(Folder), null ); IsExpandedProperty = UIHelper.RegisterDependencyProperty ( true, false, false, false, true, isExpandedPropertyChangedCallback, "IsExpanded", typeof(bool), typeof(Folder), null ); ExpandButtonVisibilityProperty = UIHelper.RegisterDependencyProperty ( Visibility.Visible, false, false, false, true, null, "ExpandButtonVisibility", typeof(Visibility), typeof(Folder), null ); CloseButtonVisibilityProperty = UIHelper.RegisterDependencyProperty ( Visibility.Visible, false, false, false, true, null, "CloseButtonVisibility", typeof(Visibility), typeof(Folder), null ); ButtonWidthProperty = UIHelper.RegisterDependencyProperty ( 20d, false, false, false, true, null, "ButtonWidth", typeof(double), typeof(Folder), null ); ButtonHeightProperty = UIHelper.RegisterDependencyProperty ( 20d, false, false, false, true, null, "ButtonHeight", typeof(double), typeof(Folder), null ); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Folder() /// <summary> /// 생성자 /// </summary> public Folder() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 확장 여부 속성 변경시 콜백 처리하기 - isExpandedPropertyChangedCallback(dependencyObject, e) /// <summary> /// 확장 여부 속성 변경시 콜백 처리하기 /// </summary> /// <param name="dependencyObject">의존 객체</param> /// <param name="e">이벤트 인자</param> private static void isExpandedPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { Folder expanderPanel = dependencyObject as Folder; bool newValue = (bool)e.NewValue; if(expanderPanel.AutoHeight) { if(newValue) { expanderPanel.Height = expanderPanel.originalHeight; } else { expanderPanel.originalHeight = expanderPanel.ActualHeight; expanderPanel.Height = expanderPanel.HeaderHeight + 2; } } expanderPanel.FireIsExpandedChanged(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Protected #region 확장 여부 변경시 이벤트 발생시키기 - FireIsExpandedChanged() /// <summary> /// 확장 여부 변경시 이벤트 발생시키기 /// </summary> protected void FireIsExpandedChanged() { if(IsExpandedChanged != null) { IsExpandedChanged(this, EventArgs.Empty); } } #endregion #region Expand 버튼 클릭시 이벤트 발생시키기 - FireExpandButtonClick() /// <summary> /// Expand 버튼 클릭시 이벤트 발생시키기 /// </summary> protected void FireExpandButtonClick() { if(ExpandButtonClick != null) { ExpandButtonClick(this, EventArgs.Empty); } } #endregion #region Close 버튼 클릭시 이벤트 발생시키기 - FireCloseButtonClick() /// <summary> /// Close 버튼 클릭시 이벤트 발생시키기 /// </summary> protected void FireCloseButtonClick() { if(CloseButtonClick != null) { CloseButtonClick(this, EventArgs.Empty); } } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region Expand 버튼 클릭시 처리하기 - expandButton_Click(sender, e) /// <summary> /// Expand 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void expandButton_Click(object sender, RoutedEventArgs e) { IsExpanded = !IsExpanded; FireExpandButtonClick(); } #endregion #region Close 버튼 클릭시 처리하기 - closeButton_Click(sender, e) /// <summary> /// Close 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void closeButton_Click(object sender, RoutedEventArgs e) { FireCloseButtonClick(); } #endregion } } |
▶ FolderContainer.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<UserControl x:Class="TestProject.FolderContainer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignWidth="250" d:DesignHeight="400"> <Grid Name="grid"> <Grid.RowDefinitions /> </Grid> </UserControl> |
▶ FolderContainer.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 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 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; namespace TestProject { /// <summary> /// 폴더 컨테이너 /// </summary> public partial class FolderContainer : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 폴더 정보 컬렉션 초기화시 - InitializeFolderInfoCollection /// <summary> /// 폴더 정보 컬렉션 초기화시 /// </summary> public event FolderInfoCollectionEventHandler InitializeFolderInfoCollection; #endregion #region 폴더 자식 생성시 - CreateFolderChild /// <summary> /// 폴더 자식 생성시 /// </summary> public event FolderInfoEventHandler CreateFolderChild; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 컬렉션 /// </summary> private FolderInfoCollection collection; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 표시 폴더 수 - VisibleFolderCount /// <summary> /// 표시 폴더 수 /// </summary> public int VisibleFolderCount { get { if(this.collection == null || this.collection.Count == 0) { return 0; } int visibleCount = 0; foreach(FolderInfo info in this.collection) { if(info.Visible) { visibleCount++; } } return visibleCount; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - FolderContainer() /// <summary> /// 생성자 /// </summary> public FolderContainer() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 초기화 하기 - Initialize() /// <summary> /// 초기화 하기 /// </summary> public void Initialize() { if(this.collection == null) { this.collection = new FolderInfoCollection(); FireInitializeFolderInfoCollection(this.collection); } for(int i = 0; i < this.collection.Count; i++) { FolderInfo info = this.collection[i]; if(i == 0) { info.RowDefinition = GetRowDefinition(21d, 1d); info.GridSplitter = null; info.Folder = GetFolder(new Thickness(0d), info.HeaderText); } else { info.RowDefinition = GetRowDefinition(24d, 1d); info.GridSplitter = GetGridSplitter(); info.Folder = GetFolder(new Thickness(0d, 3d, 0d, 0d), info.HeaderText); } } foreach(FolderInfo info in this.collection) { if(info.GridSplitter != null) { info.GridSplitter.DragStarted += gridSplitter_DragStarted; info.GridSplitter.DragCompleted += gridSplitter_DragCompleted; } if(info.Folder != null) { info.Folder.ExpandButtonClick += folder_ExpandButtonClick; info.Folder.CloseButtonClick += folder_CloseButtonClick; } } } #endregion #region 폴더 정보 구하기 - GetFolderInfo(key) /// <summary> /// 폴더 정보 구하기 /// </summary> /// <param name="key">키</param> /// <returns>폴더 정보</returns> public FolderInfo GetFolderInfo(string key) { if(this.collection == null || this.collection.Count == 0) { return null; } foreach(FolderInfo info in this.collection) { if(info.Key.Equals(key)) { return info; } } return null; } #endregion #region 폴더 정보 구하기 - GetFolderInfo(folder) /// <summary> /// 폴더 정보 구하기 /// </summary> /// <param name="folder">폴더</param> /// <returns>폴더 정보</returns> public FolderInfo GetFolderInfo(Folder folder) { if(this.collection == null || this.collection.Count == 0) { return null; } foreach(FolderInfo info in this.collection) { if(info.Folder.Equals(folder)) { return info; } } return null; } #endregion #region 폴더 보여주기 - ShowFolder(button) /// <summary> /// 폴더 보여주기 /// </summary> /// <param name="key">키</param> /// <param name="button">버튼</param> public void ShowFolder(string key, ToggleButton button) { FolderInfo folderInfo = GetFolderInfo(key); if(folderInfo == null) { return; } if(button.IsChecked.GetValueOrDefault()) { folderInfo.Visible = true; FireCreateFolderChild(folderInfo); } else { folderInfo.Visible = false; RemoveFolderChild(folderInfo); } SetFolderVisible(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected //////////////////////////////////////////////////////////////////////////////// Function #region 폴더 정보 컬렉션 초기화시 이벤트 발생시키기 - FireInitializeFolderInfoCollection(collection) /// <summary> /// 폴더 정보 컬렉션 초기화시 이벤트 발생시키기 /// </summary> /// <param name="collection">폴더 정보 컬렉션</param> protected void FireInitializeFolderInfoCollection(FolderInfoCollection collection) { if(InitializeFolderInfoCollection != null) { InitializeFolderInfoCollection(this, new FolderInfoCollectionEventArgs(collection)); } } #endregion #region 폴더 자식 생성시 이벤트 발생시키기 - FireCreateFolderChild(info) /// <summary> /// 폴더 자식 생성시 이벤트 발생시키기 /// </summary> /// <param name="info">폴더 정보</param> protected void FireCreateFolderChild(FolderInfo info) { if(CreateFolderChild != null) { CreateFolderChild(this, new FolderInfoEventArgs(info)); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 그리드 스플리터 드래그 시작시 처리하기 - gridSplitter_DragStarted(sender, e) /// <summary> /// 그리드 스플리터 드래그 시작시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridSplitter_DragStarted(object sender, DragStartedEventArgs e) { GridSplitter gridSplitter = sender as GridSplitter; if(gridSplitter == null) { return; } foreach(FolderInfo info in this.collection) { if(info.GridSplitter == null) { continue; } if(!info.GridSplitter.Equals(gridSplitter)) { continue; } double totalHeight; FolderInfo previousInfo = GetPreviousFolderInfo(this.collection, info); if(info.Folder.IsExpanded == false) { if(previousInfo != null) { if(previousInfo.Folder.IsExpanded == false) { totalHeight = GetTotalRowHeight(this.collection, null); previousInfo.RowDefinition.Height = new GridLength(previousInfo.ActualRowHeight / totalHeight, GridUnitType.Pixel); previousInfo.Folder.IsExpanded = true; previousInfo.Folder.VerticalAlignment = VerticalAlignment.Stretch; previousInfo.Folder.Height = double.NaN; e.Handled = true; return; } } totalHeight = GetTotalRowHeight(this.collection, null); info.RowDefinition.Height = new GridLength(info.ActualRowHeight / totalHeight, GridUnitType.Star); info.Folder.IsExpanded = true; info.Folder.VerticalAlignment = VerticalAlignment.Stretch; info.Folder.Height = double.NaN; } else { if(previousInfo != null) { if(previousInfo.Folder.IsExpanded == false) { totalHeight = GetTotalRowHeight(this.collection, null); previousInfo.RowDefinition.Height = new GridLength(previousInfo.ActualRowHeight / totalHeight, GridUnitType.Star); previousInfo.Folder.IsExpanded = true; previousInfo.Folder.VerticalAlignment = VerticalAlignment.Stretch; previousInfo.Folder.Height = double.NaN; e.Handled = true; return; } } } } } #endregion #region 그리드 스플리터 드래그 완료시 처리하기 - gridSplitter_DragCompleted(sender, e) /// <summary> /// 그리드 스플리터 드래그 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridSplitter_DragCompleted(object sender, DragCompletedEventArgs e) { List<FolderInfo> visibleList = new List<FolderInfo>(); double totalHeight = GetTotalRowHeight(this.collection, visibleList); foreach(FolderInfo info in visibleList) { if(info.RowDefinition.Height.GridUnitType == GridUnitType.Auto) { continue; } info.RowDefinition.Height = new GridLength(info.ActualRowHeight / totalHeight, GridUnitType.Star); } visibleList.Clear(); visibleList = null; } #endregion #region 폴더 Expand 버튼 클릭시 처리하기 - folder_ExpandButtonClick(sender, e) /// <summary> /// 폴더 Expand 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void folder_ExpandButtonClick(object sender, EventArgs e) { Folder folder = sender as Folder; if(folder == null) { return; } FolderInfo info = GetFolderInfo(folder); if(info == null) { return; } if(info.Folder.IsExpanded) { info.RowDefinition.Height = new GridLength(info.RowHeight, GridUnitType.Star); info.Folder.VerticalAlignment = VerticalAlignment.Stretch; info.Folder.Height = double.NaN; } else { info.RowHeight = info.RowDefinition.Height.Value; info.Folder.Height = 20 + 1; info.RowDefinition.Height = new GridLength(0d, GridUnitType.Auto); } } #endregion #region 폴더 Close 버튼 클릭시 처리하기 - folder_CloseButtonClick(sender, e) /// <summary> /// 폴더 Close 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void folder_CloseButtonClick(object sender, EventArgs e) { Folder folder = sender as Folder; if(folder == null) { return; } FolderInfo info = GetFolderInfo(folder); if(info == null) { return; } if(info.RowDefinition.Height.GridUnitType == GridUnitType.Auto) { info.RowDefinition.Height = new GridLength(1d, GridUnitType.Star); info.Folder.IsExpanded = true; info.Folder.VerticalAlignment = VerticalAlignment.Stretch; info.Folder.Height = double.NaN; } info.Button.IsChecked = false; info.Button.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent)); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 이전 폴더 정보 구하기 - GetPreviousFolderInfo(collection, info) /// <summary> /// 이전 폴더 정보 구하기 /// </summary> /// <param name="collection">폴더 정보 컬렉션</param> /// <param name="info">폴더 정보</param> /// <returns>이전 폴더 정보</returns> private FolderInfo GetPreviousFolderInfo(ObservableCollection<FolderInfo> collection, FolderInfo info) { int index = collection.IndexOf(info); if(index < 0) { return null; } for(int i = index - 1; i > -1; i--) { if(collection[i].Visible) { return collection[i]; } } return null; } #endregion #region 다음 폴더 정보 구하기 - GetNextFolderInfo(collection, info) /// <summary> /// 다음 폴더 정보 구하기 /// </summary> /// <param name="collection">폴더 정보 컬렉션</param> /// <param name="info">폴더 정보</param> /// <returns>다음 폴더 정보</returns> private FolderInfo GetNextFolderInfo(ObservableCollection<FolderInfo> collection, FolderInfo info) { int index = collection.IndexOf(info); if(index < 0) { return null; } for(int i = index + 1; i < collection.Count; i++) { if(collection[i].Visible) { return collection[i]; } } return null; } #endregion #region 전체 행 높이 구하기 - GetTotalRowHeight(collection, visibleList) /// <summary> /// 전체 행 높이 구하기 /// </summary> /// <param name="collection">폴더 정보 컬렉션</param> /// <param name="visibleList">표시 폴더 정보 리스트</param> /// <returns>전체 행 높이</returns> private double GetTotalRowHeight(ObservableCollection<FolderInfo> collection, List<FolderInfo> visibleList) { double totalHeight = 0d; foreach(FolderInfo info in collection) { if(info.Visible) { if(visibleList != null) { visibleList.Add(info); } info.ActualRowHeight = info.RowDefinition.ActualHeight; totalHeight += info.ActualRowHeight; } } return totalHeight; } #endregion #region 행 정의 구하기 - GetRowDefinition(minHeight, height) /// <summary> /// 행 정의 구하기 /// </summary> /// <param name="minHeight">최소 높이</param> /// <param name="height">높이</param> /// <returns>행 정의</returns> private RowDefinition GetRowDefinition(double minHeight, double height) { RowDefinition rowDefinition = new RowDefinition(); rowDefinition.MinHeight = minHeight; rowDefinition.Height = new GridLength(height, GridUnitType.Star); return rowDefinition; } #endregion #region 그리드 스플리터 구하기 - GetGridSplitter() /// <summary> /// 그리드 스플리터 구하기 /// </summary> /// <returns>그리드 스플리터</returns> private GridSplitter GetGridSplitter() { GridSplitter gridSplitter = new GridSplitter(); gridSplitter.HorizontalAlignment = HorizontalAlignment.Stretch; gridSplitter.VerticalAlignment = VerticalAlignment.Top; gridSplitter.Height = 3d; gridSplitter.ResizeBehavior = GridResizeBehavior.PreviousAndCurrent; gridSplitter.Background = SystemColors.ControlDarkBrush; return gridSplitter; } #endregion #region 폴더 헤더 브러시 구하기 - GetFolderHeaderBrush() /// <summary> /// 폴더 헤더 브러시 구하기 /// </summary> /// <returns>폴더 헤더 브러시</returns> private LinearGradientBrush GetFolderHeaderBrush() { LinearGradientBrush brush = new LinearGradientBrush(); brush.StartPoint = new Point(0, 0); brush.EndPoint = new Point(0, 1); brush.GradientStops.Add(new GradientStop(Color.FromRgb(149, 177, 220), 0d )); brush.GradientStops.Add(new GradientStop(Color.FromRgb(70 , 114, 182), 0.33d)); brush.GradientStops.Add(new GradientStop(Color.FromRgb(90 , 135, 197), 0.77d)); brush.GradientStops.Add(new GradientStop(Color.FromRgb(132, 169, 219), 1d )); return brush; } #endregion #region 폴더 구하기 - GetFolder(margin, headerText) /// <summary> /// 폴더 구하기 /// </summary> /// <param name="margin">마진</param> /// <param name="headerText">헤더 텍스트</param> /// <returns>폴더</returns> private Folder GetFolder(Thickness margin, string headerText) { Folder folder = new Folder(); folder.HorizontalAlignment = HorizontalAlignment.Stretch; folder.VerticalAlignment = VerticalAlignment.Stretch; folder.Margin = margin; folder.MinHeight = 20d; folder.Height = double.NaN; folder.HeaderHeight = 20d; folder.HeaderBackground = GetFolderHeaderBrush(); folder.HeaderText = headerText; folder.ButtonWidth = 16; folder.ButtonHeight = 16; return folder; } #endregion #region 폴더 표시 설정하기 - SetFolderVisible() /// <summary> /// 폴더 표시 설정하기 /// </summary> private void SetFolderVisible() { foreach(FolderInfo info in this.collection) { if(info.GridSplitter != null) { if(grid.Children.Contains(info.GridSplitter)) { grid.Children.Remove(info.GridSplitter); } } if(grid.Children.Contains(info.Folder)) { grid.Children.Remove(info.Folder); } } this.grid.RowDefinitions.Clear(); int index = 0; foreach(FolderInfo info in this.collection) { if(info.Visible == true) { info.Folder.ExpandButtonVisibility = Visibility.Visible; grid.RowDefinitions.Add(info.RowDefinition); if(info.GridSplitter != null && index > 0) { grid.Children.Add(info.GridSplitter); Grid.SetRow(info.GridSplitter, index); } if(index == 0) { info.Folder.Margin = new Thickness(0d); grid.Children.Add(info.Folder); } else { info.Folder.Margin = new Thickness(0d, 3d, 0d, 0d); grid.Children.Add(info.Folder); } Grid.SetRow(info.Folder, index); index++; } } if(index == 1) { foreach(FolderInfo info in this.collection) { if(info.Visible == true) { info.Folder.ExpandButtonVisibility = Visibility.Hidden; } } } } #endregion #region 폴더 자식 제거하기 - RemoveFolderChild(info) /// <summary> /// 폴더 자식 제거하기 /// </summary> /// <param name="info">차트 패널 정보</param> private void RemoveFolderChild(FolderInfo info) { info.Folder.Content = null; info.FolderChild = null; } #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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:TestProject" Width="800" Height="600" Title="폴더 컨테이너 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Name="folderContainerColumnDefinition" Width="0" /> </Grid.ColumnDefinitions> <Canvas Grid.Column="0" Margin="5" Background="Green"> <ToggleButton Name="button1" Canvas.Left="10" Canvas.Top="50" Padding="10" Content="데이터 1" Click="button_Click" /> <ToggleButton Name="button2" Canvas.Left="10" Canvas.Top="90" Padding="10" Content="데이터 2" Click="button_Click" /> <ToggleButton Name="button3" Canvas.Left="10" Canvas.Top="130" Padding="10" Content="데이터 3" Click="button_Click" /> <ToggleButton Name="button4" Canvas.Left="10" Canvas.Top="170" Padding="10" Content="데이터 4" Click="button_Click" /> </Canvas> <GridSplitter Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="3" Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" /> <src:FolderContainer x:Name="folderContainer" Grid.Column="1" Margin="3 0 0 0" /> </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 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 |
using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Media; using System.Windows.Shapes; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 폴더 컨테이너 너비 /// </summary> private double folderContainerWidth = 250d; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); #region 이벤트를 설정합니다. this.folderContainer.InitializeFolderInfoCollection += folderContainer_InitializeFolderInfoCollection; this.folderContainer.CreateFolderChild += folderContainer_CreateFolderChild; #endregion this.folderContainer.Initialize(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폴더 컨테이너 폴더 정보 컬렉션 초기화시 처리하기 - folderContainer_InitializeFolderInfoCollection(sender, e) /// <summary> /// 폴더 컨테이너 폴더 정보 컬렉션 초기화시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void folderContainer_InitializeFolderInfoCollection(object sender, FolderInfoCollectionEventArgs e) { e.Collection.Add(new FolderInfo() { Key = "button1", HeaderText = "데이터 1", Button = this.button1 }); e.Collection.Add(new FolderInfo() { Key = "button2", HeaderText = "데이터 2", Button = this.button2 }); e.Collection.Add(new FolderInfo() { Key = "button3", HeaderText = "데이터 3", Button = this.button3 }); e.Collection.Add(new FolderInfo() { Key = "button4", HeaderText = "데이터 4", Button = this.button4 }); } #endregion #region 폴더 컨테이너 폴더 자식 생성시 처리하기 - folderContainer_CreateFolderChild(sender, e) /// <summary> /// 폴더 컨테이너 폴더 자식 생성시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void folderContainer_CreateFolderChild(object sender, FolderInfoEventArgs e) { switch(e.Info.Key) { case "button1" : { Ellipse ellipse = new Ellipse(); ellipse.Fill = Brushes.Red; e.Info.FolderChild = ellipse; e.Info.Folder.Content = e.Info.FolderChild; break; } case "button2" : { Ellipse ellipse = new Ellipse(); ellipse.Fill = Brushes.Green; e.Info.FolderChild = ellipse; e.Info.Folder.Content = e.Info.FolderChild; break; } case "button3" : { Ellipse ellipse = new Ellipse(); ellipse.Fill = Brushes.Blue; e.Info.FolderChild = ellipse; e.Info.Folder.Content = e.Info.FolderChild; break; } case "button4" : { Ellipse ellipse = new Ellipse(); ellipse.Fill = Brushes.Magenta; e.Info.FolderChild = ellipse; e.Info.Folder.Content = e.Info.FolderChild; break; } } } #endregion #region 버튼 클릭시 처리하기 - button_Click(sender, e) /// <summary> /// 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_Click(object sender, RoutedEventArgs e) { ToggleButton button = sender as ToggleButton; this.folderContainer.ShowFolder(button.Name, button); if(this.folderContainer.VisibleFolderCount > 0) { this.folderContainerColumnDefinition.Width = new GridLength(this.folderContainerWidth, GridUnitType.Pixel); } else { this.folderContainerWidth = this.folderContainerColumnDefinition.ActualWidth; this.folderContainerColumnDefinition.Width = new GridLength(0d, GridUnitType.Pixel); } } #endregion } } |