'WPF 자동 정렬'에 해당되는 글 1건

  1. 2014.01.08 WPF WrapPanel 사용하기

ㄱ. 목적

1. WPF에서 WrapPanel을 이용한 동적 객체 표시를 알아본다.

2. WrapPanel의 특성을 알아본다


ㄴ. 본론

1. WrapPanel은 자식 요소를 왼쪽부터 오른쪽으로 순차적으로 배치하고, 해당 부모 컨테이너의 끝에 도달할 경우 내용을 자동으로 다음 줄로 바꾸는데 사용된다.

 콘텐스는 가로 또는 세로 방향으로 설정할 수 있다.  WrapPanel은 간단한 유동 UI(사요ㅕㅇ자 인터페이스) 시나리오에 유용하며 모든 해당 자식 요소에 동일한 크기를 적용 가능함.


2. 예제 - 버튼을 생성하여 줄이 바뀌는 것을 보는 예제

C# 코드

  1. // Create the application's main window  
  2. mainWindow = new System.Windows.Window();  
  3. mainWindow.Title = "WrapPanel Sample";  
  4.   
  5.   
  6. // Instantiate a new WrapPanel and set properties  
  7. myWrapPanel = new WrapPanel();  
  8. myWrapPanel.Background = System.Windows.Media.Brushes.Azure;  
  9. myWrapPanel.Orientation = Orientation.Horizontal;  
  10. myWrapPanel.Width = 200;  
  11. myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;  
  12. myWrapPanel.VerticalAlignment = VerticalAlignment.Top;  
  13.   
  14. // Define 3 button elements. The last three buttons are sized at width   
  15. // of 75, so the forth button wraps to the next line.  
  16. btn1 = new Button();  
  17. btn1.Content = "Button 1";  
  18. btn1.Width = 200;  
  19. btn2 = new Button();  
  20. btn2.Content = "Button 2";  
  21. btn2.Width = 75;  
  22. btn3 = new Button();  
  23. btn3.Content = "Button 3";  
  24. btn3.Width = 75;  
  25. btn4 = new Button();  
  26. btn4.Content = "Button 4";  
  27. btn4.Width = 75;  
  28.   
  29. // Add the buttons to the parent WrapPanel using the Children.Add method.  
  30. myWrapPanel.Children.Add(btn1);  
  31. myWrapPanel.Children.Add(btn2);  
  32. myWrapPanel.Children.Add(btn3);  
  33. myWrapPanel.Children.Add(btn4);  
  34.   
  35. // Add the WrapPanel to the MainWindow as Content  
  36. mainWindow.Content = myWrapPanel;  
  37. mainWindow.Show();  

XAML

  1. <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="WrapPanel Sample">  
  2.   <Border HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="2">  
  3.         <WrapPanel Background="LightBlue" Width="200" Height="100">  
  4.             <Button Width="200">Button 1</Button>  
  5.             <Button>Button 2</Button>  
  6.             <Button>Button 3</Button>  
  7.             <Button>Button 4</Button>  
  8.         </WrapPanel>  
  9.   </Border>      
  10. </Page>  


결과 화면

일반적인 WrapPanel 요소


ㄷ. 결론 

1. WrapPanel을 이용하여 간단한 나열식의 UI생성을 살펴보았다.

Posted by 시크한공돌이
,