■ MultiBinding 엘리먼트의 Converter 속성에서 IMultiValueConverter 인터페이스 구현 객체를 사용해 바인딩하는 방법을 보여준다.
▶ PersonName.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
namespace TestProject { /// <summary> /// 사람명 /// </summary> public class PersonName { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get; set; } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PersonName(firstName, lastName) /// <summary> /// 생성자 /// </summary> /// <param name="firstName">이름</param> /// <param name="lastName">성</param> public PersonName(string firstName, string lastName) { FirstName = firstName; LastName = lastName; } #endregion } } |
▶ PersonNameCollection.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 사람명 컬렉션 /// </summary> public class PersonNameCollection : ObservableCollection<PersonName> { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PersonNameCollection() /// <summary> /// 생성자 /// </summary> public PersonNameCollection() { Add(new PersonName("Willa" , "Cather" )); Add(new PersonName("Isak" , "Dinesen")); Add(new PersonName("Victor", "Hugo" )); Add(new PersonName("Jules" , "Verne" )); } #endregion } } |
▶ PersonNameConverter.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
using System; using System.Globalization; using System.Windows.Data; namespace TestProject { /// <summary> /// 사람명 변환자 /// </summary> public class PersonNameConverter : IMultiValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValueArray, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValueArray">소스 값 배열</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object[] sourceValueArray, Type targetType, object parameter, CultureInfo cultureInfo) { string name; switch((string)parameter) { case "FormatLastFirst" : name = sourceValueArray[1] + ", " + sourceValueArray[0]; break; default : name = sourceValueArray[0] + " " + sourceValueArray[1]; break; } return name; } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetTypeArray, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetTypeArray">타겟 타입 배열</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> public object[] ConvertBack(object sourceValue, Type[] targetTypeArray, object parameter, CultureInfo cultureInfo) { string[] targetValueArray = ((string) sourceValue).Split(' '); return targetValueArray; } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:PersonNameCollection x:Key="PersonNameCollectionKey" /> <local:PersonNameConverter x:Key="PersonNameConverterKey" /> <DataTemplate x:Key="PersonNameTemplateKey"> <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource PersonNameConverterKey}"> <Binding Path="FirstName" /> <Binding Path="LastName" /> </MultiBinding> </TextBlock.Text> </TextBlock> </DataTemplate> <Style TargetType="{x:Type TextBlock}"> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="Width" Value="120" /> <Setter Property="Background" Value="Silver" /> </Style> </Window.Resources> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock Width="Auto" Margin="10" Background="White" FontSize="18" FontWeight="Bold"> MultiBinding Sample </TextBlock> <ListBox Margin="0 10 0 0" Width="200" ItemTemplate="{StaticResource PersonNameTemplateKey}" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource PersonNameCollectionKey}}" /> <TextBlock Margin="0 10 0 0" Background="White"> Normal Format : </TextBlock> <TextBlock Margin="0 10 0 0" DataContext="{StaticResource PersonNameCollectionKey}"> <TextBlock.Text> <MultiBinding ConverterParameter="FormatNormal" Converter="{StaticResource PersonNameConverterKey}"> <Binding Path="FirstName" /> <Binding Path="LastName" /> </MultiBinding> </TextBlock.Text> </TextBlock> <TextBlock Margin="0 10 0 0" Background="White"> Last Name First Format : </TextBlock> <TextBlock Margin="0 10 0 0" DataContext="{StaticResource PersonNameCollectionKey}"> <TextBlock.Text> <MultiBinding ConverterParameter="FormatLastFirst" Converter="{StaticResource PersonNameConverterKey}"> <Binding Path="FirstName" /> <Binding Path="LastName" /> </MultiBinding> </TextBlock.Text> </TextBlock> </StackPanel> </Window> |