데이터 템플릿을 사용한 리스트 바인딩 - 컬렉션(리스트) 데이터 바인딩
.NET프로그래밍/Silverlight 3.0 2009. 12. 11. 09:41 |아래그림과 같이 '솔루션 탐색기'에서 '새 항목 추가"로 "클래스"파일인 "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;
}
}
}
'.NET프로그래밍 > Silverlight 3.0' 카테고리의 다른 글
컨트롤(요소) 바인딩 - 컨트롤과 컨트롤간의 바인딩 (0) | 2009.12.11 |
---|---|
DataTemplate로 리스트박스 꾸미기 (0) | 2009.12.11 |
바인딩 유효성 검사 (Validation) (0) | 2009.12.10 |
바인딩 방법 : Mode - INotifyPropertyChanged (0) | 2009.12.10 |
ConvertBack(역변환) (0) | 2009.12.10 |