아래그림과 같이 '솔루션 탐색기'에서 '새 항목 추가"로 "클래스"파일인 "ProductEntity.cs"를 추가하고 아래와 같이 코딩한다.









[ProductEntity.cs]

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace RiaCollectionBinding

{

    public class ProductEntity

    {

        public int ProductID { get; set; }

        public string ModelName { get; set; }

        public int SellPrice { get; set; }

        public string ProductImage { get; set; }

        // 리스트박스 바인딩 기본 필드를 설정

        public override string ToString() {

            return ModelName;

        }

    }

}

 

 







[MainPage.xaml]


<UserControl x:Class="RiaCollectionBinding.MainPage"

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

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

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">

 

    <Grid x:Name="LayoutRoot" Background="White">

        <Grid.RowDefinitions>

            <RowDefinition Height="100"></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>       

        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">

            <ListBox x:Name="lstProducts" Width="200" Height="120"></ListBox>

            <ListBox x:Name="lstProducts2" Width="200" Height="120"></ListBox>

        </StackPanel>

    </Grid>

</UserControl>

 

 







[MainPage.xaml.cs]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace RiaCollectionBinding

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

            InitializeData();

        }

 

        private void InitializeData()

        {

            // 3개의 상품을 등록

            List<ProductEntity> lst = new List<ProductEntity>();

            // 수작업으로 등록

            ProductEntity pe1 = new ProductEntity() {

                ProductID = 1, ModelName = "좋은컴퓨터", SellPrice = 100, ProductImage = "COM-01.jpg" };

            lst.Add(pe1);

            pe1 = new ProductEntity() {

                ProductID = 2, ModelName = "좋은책", SellPrice = 80, ProductImage = "BOOK-01.jpg" };

            lst.Add(pe1);

            pe1 = new ProductEntity() {

                ProductID = 3, ModelName = "좋은강의", SellPrice = 100, ProductImage = "SOFTWARE-01.jpg" };

            lst.Add(pe1);           

            // 리스트 컨트롤에 바인딩

            this.lstProducts.ItemsSource = lst; // 컬렉션 형태의 데이터는 ItemsSource 속성으로 바인딩

            this.lstProducts.DisplayMemberPath = "ModelName";

            this.lstProducts.SelectedIndex = 0;

 

            this.lstProducts2.ItemsSource = lst;

        }

    }

}

 

 
















Posted by holland14
: