Document directory
Meaning
Bindingbase. stringformat attribute
Gets or sets a string that specifies how to set the binding format if the binding value is displayed as a string.
Namespace:System. Windows. Data
Assembly:Presentationframework (in presentationframework. dll)
Xmlns for XAML:Http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Stringformat and Converter
If the converter and stringformat attributes are set, the converter is applied to the data value first, and then the stringformat is applied.
Use
1. When binding uses stringformat and stringformat is set to the string format, only one parameter can be specified. For example, bind Name:
<ListView ItemsSource="{StaticResource MyData}"> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding Path=Description}"/> <GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat=Now {0:c}!}"/> <GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat={}{0:c}!}"/> </GridView> </ListView.View></ListView>
Note:
If stringformat does not contain any character, add "{}" after "stringformat =".
If stringformat contains characters, you do not need to add "{}"
2. Bind the formatting time
<TextBlock Text="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" />
Or
<TextBlock Text="{Binding Time,StringFormat='yyyy:MM:dd HH:mm:ss'}"/>
3. Multiple bindings
<ListBox ItemsSource="{StaticResource MyData}"> <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>
4. Special characters in multiple bindings, such as \ t
<TextBlock.Text> <MultiBinding StringFormat="Delete {0}	{1}"> <Binding Path="FirstName" /> <Binding Path="LastName" /> </MultiBinding> </TextBlock.Text>
The special characters are as follows:
- \ A & # x07; BEL
- \ B & # x08; BS-backspace
- \ F & # x0c; FF-formfeed
- \ N & # x0a; LF, NL-linefeed, New Line
- \ R & # x0d; Cr-carriage return
- \ T & # x09; HT-tab, horizontal tabelator
- \ V & # x0b; VT-vertical tabelator
5. When prioritybinding is used, you can set stringformat on prioritybinding and/or child binding objects.
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" DataContext="{Binding Source={StaticResource AsyncDS}}"> <TextBlock FontSize="18" FontWeight="Bold" Margin="10" HorizontalAlignment="Center">Priority Binding</TextBlock> <TextBlock Background="Honeydew" Width="100" HorizontalAlignment="Center"> <TextBlock.Text> <PriorityBinding FallbackValue="defaultvalue"> <Binding Path="SlowestDP" IsAsync="True"/> <Binding Path="SlowerDP" IsAsync="True"/> <Binding Path="FastDP" /> </PriorityBinding> </TextBlock.Text> </TextBlock> </StackPanel>
For more information, see: http://msdn.microsoft.com/zh-cn/library/vstudio/system.windows.data.prioritybinding.aspx