본문 바로가기

   
Programming/WPF

StackPanel, ScrollViewer, WARPPanelXAML, Grid XAML, UniformGrid, Canvas, DockPanel

반응형




 StackPanel XAML로 생성 
<Window x:Class="WPFEx.Panel"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Panel" Height="300" Width="300">

       <Window.Content>

             <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100">

                    <StackPanel.Children>

                           <Button Content="버튼"></Button>

                           <Button Content="버튼"></Button>

                           <Button Content="버튼"></Button>

                           <Button Content="버튼"></Button>

                           <Button Content="버튼"></Button>

                           <Button Content="버튼"></Button>

                    </StackPanel.Children>

             </StackPanel>

       </Window.Content>

      

      

</Window>

<!--

       윈폼 : 컨트롤 배치 : Location(Point)

       WPF : 컨트롤 배치 : Panel를 사용(6종류)

       패널 : 각각의 패널은 각자 자신의 자식들을 어떤 식으로 정렬해야 할지 이미 결정 O

       1. StackPanel : 일렬로 배치

       2. WrapPanel : 일렬로 배치

       3. Grid : 표 배치

       4. UniformGrid : 표 배치

       5. Canvas : 좌표 배치

       6. DockPanel : 채워넣기

      

       패널의 공통점 : 자식을 1개 이상 가질 수 있고, 자식들을 Children 속성을 통해서 관리(컬렉션)

      

       하나의 컨트롤이 자식을 가질떄..

       1. 자식을 1개만 가지는 경우 : Content 속성O (Window 객체는 자식을 하나만 가질수 있다.

       2. 자식을 1개 이상 가지는 경우 : Children, Items, Dictionary 등의 속성 O

-->

 StackPanel CS로 생성
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

 

namespace WPFEx

{

       /// <summary>

       /// StackPanel02.xaml에 대한 상호 작용 논리

       /// </summary>

       public partial class StackPanel02 : Window

       {

             public StackPanel02()

             {

                    InitializeComponent();

             }

 

             private void Window_Loaded(object sender, RoutedEventArgs e)

             {

                    //1. StackPanel 생성

                    StackPanel stack = new StackPanel();

                    stack.HorizontalAlignment = HorizontalAlignment.Center;

                    stack.VerticalAlignment = VerticalAlignment.Center;

 

                    stack.Orientation = Orientation.Vertical;

                    stack.Width = 100;

                    stack.Background = new SolidColorBrush(Colors.Yellow);

 

                    //2. Window의 자식으로 추가

                    this.Content = stack;

 

                    //3. StackPanel의 자식으로 Button x5개 추가

                    for (int i = 0; i < 5; i++)

                    {

                           Button btn = new Button();

                           btn.Content = "버튼" + i.ToString();

                           stack.Children.Add(btn);

                    }

             }

       }

}

 
ScrollViewer


ScrollViewer XAML

<Window x:Class="WPFEx.ScrollViewer"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="ScrollViewer" Height="300" Width="300">

       <ScrollViewer Width="200" Height="300" VerticalScrollBarVisibility="Auto">

             <StackPanel>

                    <StackPanel.Children>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    </StackPanel.Children>

             </StackPanel>

       </ScrollViewer>

</Window>

<!--

       ScrollViewer : 패널의 내용물이 일정 크기 이상되었을때 스크롤바를 생성 역할

-->



WarpPanel XAML

<Window x:Class="WPFEx.WrapPanel"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="WrapPanel" Height="300" Width="300">

 

       <WrapPanel Orientation="Vertical" ItemHeight="50" ItemWidth="50">

             <WrapPanel.Children>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

             </WrapPanel.Children>

       </WrapPanel>

      

</Window>

<!--

       왼쪽~오른쪽, ~아래 배치

       자동 개행**

-->

 Grid XAML
 

<Window x:Class="WPFEx.Grid"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Grid" Height="300" Width="300">

       <!--왼쪽 위 오른쪽 아래-->

       <!--<Grid>

             <Grid.Children>

                    <Button Background="Yellow">버튼1</Button>

                    <Button Background="Red" Width="100" Height="100">버튼2</Button>

                    <Button Width="50" Height="50">버튼3</Button>

             </Grid.Children>

       </Grid>-->

 

       <!--<Grid>

             <Grid.Children>

                    <Button Margin="50, 10, 100, 0">버튼</Button>

             </Grid.Children>

       </Grid>-->

 

       <!--<Grid>

              <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="48,62,0,0" Name="button1" VerticalAlignment="Top" Width="75" />

       </Grid>-->

 

       <!--<Grid>

             <Button Width="50" Height="50" Margin="10,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">버튼1</Button>

             <Button Width="50" Height="50" Margin="150,150,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">버튼2</Button>

       </Grid>-->

 

       <Grid ShowGridLines="True">

             <!--표형식 : 2x2-->

             <Grid.RowDefinitions>

                    <RowDefinition Height="50" />

                    <!--행하나-->

                    <RowDefinition Height="*" />

             </Grid.RowDefinitions>

             <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="50" />

                    <!--열하나-->

                    <ColumnDefinition Width="*" />

             </Grid.ColumnDefinitions>

             <Grid.Children>

                    <!--Grid.Row : 복합속성, 종속속성 -->

                    <Button Grid.Row="0" Grid.Column="0">1</Button>

                    <Button Grid.Row="0" Grid.Column="1">2</Button>

                    <Button Grid.Row="1" Grid.Column="0">3</Button>

                    <Button Grid.Row="1" Grid.Column="1">4</Button>

             </Grid.Children>

       </Grid>

</Window>

 

UniformGrid


UniformGrid Xaml 

<Window x:Class="WPFEx.UniformGrid"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="UniformGrid" Height="300" Width="300">

       <UniformGrid Columns="2">

             <UniformGrid.Children>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

                    <Button>버튼</Button>

             </UniformGrid.Children>

       </UniformGrid>

</Window>

Canvas


Canvas Xaml  

<Window x:Class="WPFEx.Canvas"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Canvas" Height="300" Width="300">

       <Canvas>

             <Canvas.Children>

                    <Button>버튼1</Button>

                    <Button Canvas.Left="50" Canvas.Top="50">버튼2</Button>

                    <Button Canvas.Right="50" Canvas.Bottom="50">버튼3</Button>

             </Canvas.Children>

       </Canvas>

</Window>


DockPanel
  

 

DockPanel Xaml

<Window x:Class="WPFEx.DockPanel"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="DockPanel" Height="300" Width="300">

       <DockPanel>

             <DockPanel.Children>

                    <Button DockPanel.Dock="Top" Height="50">버튼1</Button>

                    <Button DockPanel.Dock="Left" Width="50">버튼2</Button>

                    <Button>버튼3</Button>

             </DockPanel.Children>

       </DockPanel>

</Window>

<!--

       막내는 무조건 자동적으로 나머지를 채워준다.

-->

 

반응형