■ Binding 태그 확장과 MultiBinding 엘리먼트의 StringFormat 속성을 사용해 문자열 포맷을 설정하는 방법을 보여준다.
▶ PurchaseItem.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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
using System; namespace TestProject { /// <summary> /// 구매 항목 /// </summary> public class PurchaseItem { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 설명 - Description /// <summary> /// 설명 /// </summary> public string Description { get; set; } #endregion #region 가격 - Price /// <summary> /// 가격 /// </summary> public double Price { get; set; } #endregion #region 제안 만기일 - OfferExpires /// <summary> /// 제안 만기일 /// </summary> public DateTime OfferExpires { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PurchaseItem() /// <summary> /// 생성자 /// </summary> public PurchaseItem() { } #endregion #region 생성자 - PurchaseItem(description, price, offerExpires) /// <summary> /// 생성자 /// </summary> /// <param name="description">설명</param> /// <param name="price">가격</param> /// <param name="offerExpires">제안 만기일</param> public PurchaseItem(string description, double price, DateTime offerExpires) { Description = description; Price = price; OfferExpires = offerExpires; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() => $"{Description}, {Price:c}, {OfferExpires:D}"; #endregion } } |
▶ PurchaseItemCollection.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 |
using System; using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 구매 항목 컬렉션 /// </summary> public class PurchaseItemCollection : ObservableCollection<PurchaseItem> { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PurchaseItemCollection() /// <summary> /// 생성자 /// </summary> public PurchaseItemCollection() { Add((new PurchaseItem("Snowboard and bindings" , 120 , new DateTime(2009, 1, 1 )))); Add((new PurchaseItem("Inside C#, second edition", 10 , new DateTime(2009, 2, 2 )))); Add((new PurchaseItem("Laptop - only 1 year old" , 499.99, new DateTime(2009, 2, 28)))); Add((new PurchaseItem("Set of 6 chairs" , 120 , new DateTime(2009, 2, 28)))); Add((new PurchaseItem("My DVD Collection" , 15 , new DateTime(2009, 1, 1 )))); Add((new PurchaseItem("TV Drama Series" , 39.985, new DateTime(2009, 1, 1 )))); Add((new PurchaseItem("Squash racket" , 60 , new DateTime(2009, 2, 28)))); } #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 |
<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"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10"> <StackPanel.Resources> <local:PurchaseItemCollection x:Key="PurchaseItemCollectionKey" /> </StackPanel.Resources> <TextBlock Margin="0 10 0 0" FontSize="20"> Formatting a string on a Binding </TextBlock> <TextBlock Margin="0 10 0 0" FontSize="14" TextWrapping="Wrap"> This ListView contains a list of items for sale. The second column of the ListView displays a string that includes the price of the object. The price is formatted as a currency. </TextBlock> <ListView Margin="0 10 0 0" ItemsSource="{StaticResource PurchaseItemCollectionKey}"> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding Path=Description}" /> <GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat=Now {0:c}!}" /> </GridView> </ListView.View> </ListView> <TextBlock Margin="0 10 0 0" FontSize="20"> Formatting a string on a MultiBinding </TextBlock> <TextBlock Margin="0 10 0 0" FontSize="14" TextWrapping="Wrap"> This ListBox contains a list of items for sale. The ListBox displays a string for each item, which includes the Description and the Price, by setting the StringFormat property on a MultiBinding. </TextBlock> <ListBox Margin="0 10 0 0" ItemsSource="{StaticResource PurchaseItemCollectionKey}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock.Text> <MultiBinding StringFormat="{}{0} -- Now only {1:C}!"> <Binding Path="Description" /> <Binding Path="Price" /> </MultiBinding> </TextBlock.Text> </TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Window> |