01
2015
09

XAML中的Format用法


在 XAML中这样使用


                <telerik:ChartCrosshairBehavior >
                    <telerik:ChartCrosshairBehavior.VerticalLineLabelDefinition >
                        <telerik:ChartAnnotationLabelDefinition VerticalAlignment="Top" Format="{}{0:yyyy-MM-dd HH:mm:ss}"/>
                    </telerik:ChartCrosshairBehavior.VerticalLineLabelDefinition>
                </telerik:ChartCrosshairBehavior>

 


也可以这样


<TextBlock Text="{Binding Date, StringFormat=\{0:d\}}" />



如果是在后台的C#中,这样


var crosshair = (ChartCrosshairBehavior)be;
                    crosshair.VerticalLineLabelDefinition = new ChartAnnotationLabelDefinition()
                    {
                        HorizontalOffset = 2,
                        VerticalOffset = 0,
                          Format = "{0:d}"
                    };




另外附各种 StringFormat in XAML 

Strings

<Button Content=”{Binding username, StringFormat=’Log Out of \{0\} Account’}“/>

For a string with the value “Silverlight”

Using \{0\,#\} effectively forces the string to be at least # characters long, using spaces to pad it to the requested length.

StringFormat=\{0\,20\} : “                  Silverlight” 
StringFormat=\{0\,-20\} : “Silverlight                  ”
StringFormat=’I just typed &quot;\{0\}&quot;.’ : “I just typed “Silverlight”.”

Numbers

<TextBox Text=”{Binding paymentAmount, StringFormat=\{0:C2\}}“/>

For a double with the value : “38293.53”

StringFormat=c : “$38,293.53” – Use ‘c’ for currency
StringFormat=e : “3.829353e+004” – Use ‘e’ for exponential (scientific)
StringFormat=n : “38,293.53” – Use n for number

You can also use these in the following format:

\{0:(letter)(number)\}

where (number) indicates how many decimal places there should be. The format will use standard rounding rules to determine the last digit. For example:

StringFormat=\{0:c0\} : “$38,294”
StringFormat=\{0:n4\} : “38,293.5300”
StringFormat=You have \{0:c1\} : “You have $38,293.5”

Dates

The date formatting has a huge range of options.

For the DateTime of “April 17, 2004, 1:52:45 PM”

You can either use a set of standard formats (standard formats)…
StringFormat=f : “Saturday, April 17, 2004 1:52 PM”
StringFormat=g : “4/17/2004 1:52 PM”
StringFormat=m : “April 17”
StringFormat=y : “April, 2004”
StringFormat=t : “1:52 PM”
StringFormat=u : “2004-04-17 13:52:45Z”
StringFormat=o : “2004-04-17T13:52:45.0000000”

… or you can create your own date formatting using letters (custom formats)

StringFormat=’MM/dd/yy’ : “04/17/04”
StringFormat=’MMMM dd, yyyy g’ : “April 17, 2004 A.D.”
StringFormat=’hh:mm:ss.fff tt’ : “01:52:45.000 PM”

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。