컨트롤(요소) 바인딩 - 컨트롤과 컨트롤간의 바인딩
컨트롤과 컨트롤간에 직접 바인딩 제공
앞에서했던 "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;
}
}
}