컨트롤(요소) 바인딩 - 컨트롤과 컨트롤간의 바인딩
.NET프로그래밍/Silverlight 3.0 2009. 12. 11. 09:54 |컨트롤과 컨트롤간에 직접 바인딩 제공
앞에서했던 "RiaDataTemplate"파일을 복사하여 "RiaControlBinding"로 폴더명을 바꾸고 프로젝트를 열어서 실습한다.
[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="400"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<ListBox x:Name="lstProducts" Width="400" Height="400" Margin="10">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=ProductID}" Foreground="Red" Margin="3" />
<Image Source="{Binding ProductImage}" Margin="2" Height="80" Width="80" />
<TextBlock Text="{Binding ModelName}" Margin="3" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox x:Name="lstProducts2" Width="200" Height="120"></ListBox>
</StackPanel>
<!-- 컨트롤과 컨트롤간의 바인딩 -->
<StackPanel Grid.Row="1">
<TextBlock Text="{Binding SelectedItem.ProductID, ElementName=lstProducts2}" />
<TextBlock Text="{Binding SelectedItem.ModelName, ElementName=lstProducts2}" />
<Image Source="{Binding SelectedItem.ProductImage, ElementName=lstProducts2}"
Width="100" Height="100" />
<TextBlock Text="{Binding SelectedItem.SellPrice, ElementName=lstProducts2}" />
</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;
}
}
}
[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;
}
}
}
'.NET프로그래밍 > Silverlight 3.0' 카테고리의 다른 글
DataGrid - 현재 선택된 행 가져오기 (0) | 2009.12.11 |
---|---|
DataGrid 컨트롤로 데이터 출력 (0) | 2009.12.11 |
DataTemplate로 리스트박스 꾸미기 (0) | 2009.12.11 |
데이터 템플릿을 사용한 리스트 바인딩 - 컬렉션(리스트) 데이터 바인딩 (0) | 2009.12.11 |
바인딩 유효성 검사 (Validation) (0) | 2009.12.10 |