첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ ListBox 엘리먼트 : ItemsPanel 속성을 사용해 수평으로 항목 나열하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip



MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

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

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

    Width="800"

    Height="600"

    Title="ListBox 엘리먼트 : ItemsPanel 속성을 사용해 수평으로 항목 나열하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid

        HorizontalAlignment="Center"

        VerticalAlignment="Center">

        <Grid.RowDefinitions>

            <RowDefinition />

            <RowDefinition />

        </Grid.RowDefinitions>

        <Grid.Resources>

            <Style TargetType="{x:Type Separator}">

                <Setter Property="Template">

                    <Setter.Value>

                        <ControlTemplate TargetType="{x:Type Separator}">

                            <Border

                                Margin="4"

                                Width="2"

                                Height="12"

                                Background="Gray" />

                        </ControlTemplate>

                    </Setter.Value>

                </Setter>

            </Style>

            <Style TargetType="ListBox">

                <Setter Property="ItemsPanel">

                    <Setter.Value>

                        <ItemsPanelTemplate>

                            <StackPanel

                                VerticalAlignment="Center"

                                HorizontalAlignment="Center"

                                Orientation="Horizontal" />

                        </ItemsPanelTemplate>

                    </Setter.Value>

                </Setter>

            </Style>

        </Grid.Resources>

        <ListBox Name="listBox" Grid.Row="0" Grid.Column="0"

            Margin="10"

            Height="50"

            SelectionChanged="listBox_SelectionChanged">

            <ListBoxItem>항목01</ListBoxItem>

            <Separator/>

            <ListBoxItem>항목02</ListBoxItem>

            <Separator/>

            <ListBoxItem>항목03</ListBoxItem>

            <Separator/>

            <ListBoxItem>항목04</ListBoxItem>

            <Separator/>

            <ListBoxItem>항목05</ListBoxItem>

            <Separator/>

            <ListBoxItem>항목06</ListBoxItem>

            <Separator/>

            <ListBoxItem>항목07</ListBoxItem>

            <Separator/>

            <ListBoxItem>항목08</ListBoxItem>

            <Separator/>

            <ListBoxItem>항목09</ListBoxItem>

            <Separator/>

            <ListBoxItem>항목10</ListBoxItem>

        </ListBox>

        <Label Name="messageLabel" Grid.Row="1" Grid.Column="0"

            Margin="10" />

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Windows.Controls;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.listBox.SelectionChanged += listBox_SelectionChanged;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 리스트 박스 선택 변경시 처리하기 - listBox_SelectionChanged(sender, e)

 

        /// <summary>

        /// 리스트 박스 선택 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            ListBox listBox = sender as ListBox;

 

            ListBoxItem listBoxItem = listBox.SelectedItem as ListBoxItem;

 

            if(listBoxItem == null)

            {

                return;

            }

 

            this.messageLabel.Content = "선택 항목 : " + listBoxItem.Content.ToString();

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요