WPF XAML之bing使用StringFormat

      释义

      BindingBase.StringFormat 属性

              获取或设置一个字符串,该字符串指定如果绑定值显示为字符串,应如何设置该绑定的格式。

       命名空间: System.Windows.Data
       程序集: PresentationFramework(在 PresentationFramework.dll 中)
       用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

      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}&#x09;{1}">
        <Binding Path="FirstName" />
        <Binding Path="LastName" />
    </MultiBinding>
 </TextBlock.Text>

 

特殊字符如下:

  • \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,在使用 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>

      关于此条更多信息看:http://msdn.microsoft.com/zh-cn/library/vstudio/system.windows.data.prioritybinding.aspx

posted @ 2013-05-26 14:37  墨梅  阅读(21590)  评论(1编辑  收藏  举报