释义BindingBase.StringFormat 属性 获取或设置一个字符串,该字符串指定如果绑定值显示为字符串,应如何设置该绑定的格式。 命名空间: System.Windows.Data StringFormat和Converter如果设置 Converter 和 StringFormat 属性,则会先对数据值应用转换器,然后应用 StringFormat。 使用1,Binding中使用StringFormat, StringFormat 设置为撰写字符串格式时,只能指定一个参数。如绑定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> 注意: 如果StringFormat中没有字符,“StringFormat=”后面需要先加入“{}”。 如果StringFormat中有字符,则不需要加入“{}” 2,绑定格式化时间 <TextBlock Text="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" /> 或者 <TextBlock Text="{Binding Time,StringFormat='yyyy:MM:dd HH:mm:ss'}"/> 3,多重绑定 <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,多重绑定中的特殊字符, 如 \t <TextBlock.Text> <MultiBinding StringFormat="Delete {0}	{1}"> <Binding Path="FirstName" /> <Binding Path="LastName" /> </MultiBinding> </TextBlock.Text>
特殊字符如下:
5,在使用 PriorityBinding 时,可以在 PriorityBinding 和/或子绑定对象上设置 StringFormat。 <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> |
|