Bing of wpf xaml uses stringformat

Source: Internet
Author: User
Document directory
  • Meaning
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}&#x09;{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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.