■ ListBox 클래스 : ItemTemplate 속성 사용하기

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


TestProject.zip


SingleItem.cs

 

 

using System.Windows.Media;

 

namespace TestProject

{

    /// <summary>

    /// 싱글 항목

    /// </summary>

    public class SingleItem

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

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

 

        #region 아이콘 - Icon

 

        /// <summary>

        /// 아이콘

        /// </summary>

        public ImageSource Icon { get; set; }

 

        #endregion

        #region 텍스트 - Text

 

        /// <summary>

        /// 텍스트

        /// </summary>

        public string Text { get; set; }

 

        #endregion

    }

}

 

 

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 클래스 : ItemTemplate 속성 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <ListBox Name="listBox"

            Width="200"

            Height="300"

            SelectionMode="Single">

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <StackPanel Orientation="Horizontal">

                        <Image Name="itemImage"

                            Margin="0 0 5 0"

                            Width="16"

                            Height="16"

                            VerticalAlignment="Center"

                            Stretch="None"

                            Source="{Binding Icon}" />

                        <TextBlock

                            VerticalAlignment="Center"

                            Text="{Binding Text}" />

                    </StackPanel>

                    <DataTemplate.Triggers>

                        <DataTrigger Binding="{Binding Icon}" Value="{x:Null}">

                            <Setter TargetName="itemImage" Property="Visibility" Value="Collapsed" />

                        </DataTrigger>

                    </DataTemplate.Triggers>

                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.Collections.ObjectModel;

using System.IO;

using System.Windows;

using System.Windows.Media.Imaging;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            Loaded += Window_Loaded;

        }

 

        #endregion

 

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

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

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

 

        /// <summary>

        /// 윈도우 로드시 처리하기

        /// </summary>

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

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

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            BitmapImage customerBitmapImage = new BitmapImage(GetResourceUri("Image/customer.png"));

            BitmapImage employeeBitmapImage = new BitmapImage(GetResourceUri("Image/employee.png"));

 

            ObservableCollection<SingleItem> collection = new ObservableCollection<SingleItem>();

 

            collection.Add(new SingleItem() { Icon = customerBitmapImage, Text = "홍길동" });

            collection.Add(new SingleItem() { Icon = employeeBitmapImage, Text = "김철수" });

            collection.Add(new SingleItem() { Icon = customerBitmapImage, Text = "이영희" });

            collection.Add(new SingleItem() { Icon = employeeBitmapImage, Text = "한강도" });

            collection.Add(new SingleItem() { Icon = customerBitmapImage, Text = "김상도" });

 

            this.listBox.ItemsSource = collection;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 리소스 URI 구하기 - GetResourceUri(resourcePath)

 

        /// <summary>

        /// 리소스 URI 구하기

        /// </summary>

        /// <param name="resourcePath">리소스 경로</param>

        /// <returns>리소스 URI</returns>

        public Uri GetResourceUri(string resourcePath)

        {

            return new Uri(string.Format("pack://application:,,,/{0}", resourcePath));

        }

 

        #endregion

        #region 리소스 스트림 구하기 - GetResourceStream(resourceUri)

 

        /// <summary>

        /// 리소스 스트림 구하기

        /// </summary>

        /// <param name="resourceUri">리소스 URI</param>

        /// <returns>리소스 스트림</returns>

        public Stream GetResourceStream(Uri resourceUri)

        {

            return Application.GetResourceStream(resourceUri).Stream;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요