BindingToStringFomat
데이터 바인딩의 결과로 문자열을 표시하려는 경우 Binding의 StringFormat 속성을 사용하면 쉽게 디스플레이를 사용자 정의 할 수 있습니다. 이 예제에서는 바인딩과 멀티 바인딩 시 사용되는 StringFormat에 대해 설명 합니다.
바인딩 객체 정의
1: public class PurchaseItem
2: {
3: public PurchaseItem() { }
4: public PurchaseItem(string desc, double price, DateTime endDate)
5: {
6: Description = desc;
7: Price = price;
8: OfferExpires = endDate;
9: }
10: public string Description { get; set; }
11: public double Price { get; set; }
12: public DateTime OfferExpires { get; set; }
13: public override string ToString()
14: {
15: return $"{Description}, {Price:C}, {OfferExpires:D}";
16: }
17: }
바인딩 컬렉션 객체 정의
1: public class ItemsForSale : ObservableCollection<PurchaseItem>
2: {
3: public ItemsForSale()
4: {
5: Add((new PurchaseItem("Snowboard and bindings", 120, new DateTime(2009, 1, 1))));
6: Add((new PurchaseItem("Inside C#, second edition", 10, new DateTime(2009, 2, 2))));
7: Add((new PurchaseItem("Laptop - only 1 year old", 499.99, new DateTime(2009, 2, 28))));
8: Add((new PurchaseItem("Set of 6 chairs", 120, new DateTime(2009, 2, 28))));
9: Add((new PurchaseItem("My DVD Collection", 15, new DateTime(2009, 1, 1))));
10: Add((new PurchaseItem("TV Drama Series", 39.985, new DateTime(2009, 1, 1))));
11: Add((new PurchaseItem("Squash racket", 60, new DateTime(2009, 2, 28))));
12: }
13: }
Binding StringFormat
{0}은 원시 대상 객체를 나타내며 통화 서식의 경우 {0 : C}, 퍼센트 서식의 경우 {0 : P}, 16 진 서식의 경우 {0 : X}와 같이 다양한 서식 지정자가 지원됩니다.
1: <ListView ItemsSource="{StaticResource MyData}">
2: <ListView.View>
3: <GridView>
4: <GridViewColumn DisplayMemberBinding="{Binding Path=Description}" />
5: <GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat=Now {0:c}!}" />
6: </GridView>
7: </ListView.View>
8: </ListView>
Multi Binding StringFormat
1: <ListBox ItemsSource="{StaticResource MyData}">
2: <ListBox.ItemTemplate>
3: <DataTemplate>
4: <TextBlock>
5: <TextBlock.Text>
6: <MultiBinding StringFormat="{}{0} -- Now only {1:C}!">
7: <Binding Path="Description" />
8: <Binding Path="Price" />
9: </MultiBinding>
10: </TextBlock.Text>
11: </TextBlock>
12: </DataTemplate>
13: </ListBox.ItemTemplate>
14: </ListBox>
전체소스
1: <Window x:Class="BindingToStringFomat.MainWindow"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: xmlns:local="clr-namespace:BindingToStringFomat"
7: mc:Ignorable="d"
8: Title="MainWindow"
9: SizeToContent="WidthAndHeight">
10: <StackPanel>
11: <StackPanel.Resources>
12: <local:ItemsForSale x:Key="MyData" />
13: </StackPanel.Resources>
14: <TextBlock Margin="5,10,0,0" FontSize="20">Formatting a string on a Binding</TextBlock>
15: <TextBlock Margin="5" FontSize="14" TextWrapping="Wrap">
16: This ListView contains a list of items for sale.
17: The second column of the ListView displays a string that
18: includes the price of the object. The price is formatted as a currency.
19: </TextBlock>
20: <ListView ItemsSource="{StaticResource MyData}">
21: <ListView.View>
22: <GridView>
23: <GridViewColumn DisplayMemberBinding="{Binding Path=Description}" />
24: <GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat=Now {0:c}!}" />
25: </GridView>
26: </ListView.View>
27: </ListView>
28: <TextBlock FontSize="20" Margin="5,10,0,0">Formatting a string on a MultiBinding</TextBlock>
29: <TextBlock Margin="5" FontSize="14" TextWrapping="Wrap">
30: This ListBox contains a list of items for sale.
31: The ListBox displays a string for each item,
32: which includes the Description and the Price,
33: by setting the StringFormat property on a MultiBinding.
34: </TextBlock>
35: <ListBox ItemsSource="{StaticResource MyData}">
36: <ListBox.ItemTemplate>
37: <DataTemplate>
38: <TextBlock>
39: <TextBlock.Text>
40: <MultiBinding StringFormat="{}{0} -- Now only {1:C}!">
41: <Binding Path="Description" />
42: <Binding Path="Price" />
43: </MultiBinding>
44: </TextBlock.Text>
45: </TextBlock>
46: </DataTemplate>
47: </ListBox.ItemTemplate>
48: </ListBox>
49: </StackPanel>
50: </Window>
댓글
댓글 쓰기