다음을 통해 공유


방법: 가로 ListBox 만들기

업데이트: 2007년 11월

이 예제에서는 스타일을 정의하여 가로 ListBox를 만드는 방법을 보여 줍니다. ListBoxItem 컨트롤은 가로로 나열되며 사용자 정의 구분 기호로 구분됩니다. 스타일은 ListBoxItemsPanel 속성을 가로 StackPanel로 설정합니다. 다음 예제에서는 SeparatorListBox의 스타일을 보여 줍니다.

예제

<Grid.Resources>
  <Style TargetType="Separator">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Separator}">
          <Border Width="2" Height="12" Margin="4" Background="Gray"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style TargetType="ListBox">
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal"
                      VerticalAlignment="Center"
                      HorizontalAlignment="Center"/>
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
  </Style>

</Grid.Resources>


...


<ListBox Name="lb" 
         Margin="10, 10, 3, 3" Height="50"
         Grid.Column="0" Grid.Row="2"
         Grid.RowSpan="2"
         SelectionChanged="PrintText">
  <ListBoxItem>Item 1</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 2</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 3</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 4</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 5</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 6</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 7</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 8</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 9</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 10</ListBoxItem>
</ListBox>

전체 샘플을 보려면 가로 ListBox 샘플을 참조하십시오.

또한 새 ControlTemplate을 만들어 가로 ListBox를 만들 수 있습니다. 자세한 내용은 ItemsPanel의 예제를 참조하십시오.