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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM