代码改变世界

Silverlight 4常用StringFormat格式总结(转)

2011-04-26 10:29  血糯米Otomii  阅读(500)  评论(0编辑  收藏  举报

 

转自:http://silverlightchina.net/html/tips/2011/0424/7149.html 作者:Jv9

1_110425100536_1

实例代码:

 

<Grid x:Name="LayoutRoot">
<ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}">
<StackPanel Orientation="Horizontal">
<StackPanel x:Name="ContentStackPanel">
<TextBlock FontSize="16" TextWrapping="Wrap" Text="StringFormat和字符串格式" Margin="8"/>
<Border BorderBrush="LightGray" BorderThickness="1" Width="300" Margin="5" HorizontalAlignment="Left">
<StackPanel Orientation="Vertical" Margin="5" >
<TextBox x:Name="txtSource" Width="125" HorizontalAlignment="Left"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat='正在输入 &quot;\{0\}&quot;." FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock Text="{Binding Text, ElementName=txtSource, StringFormat='正在输入 &quot;\{0\}&quot;.'}" TextWrapping="Wrap" Margin="0,0,0,4"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=\{0\,20\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock Text="{Binding Text, ElementName=txtSource, StringFormat=\{0\,20\}}" TextWrapping="Wrap" Margin="0,0,0,4" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=\{0\,-20\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock Text="{Binding Text, ElementName=txtSource, StringFormat=\{0\,-20\}}" TextWrapping="Wrap" Margin="0,0,0,4"/>
</StackPanel>
</Border>

<StackPanel x:Name="DatePanel">
<TextBlock Text="StringFormat和日期格式" Margin="8" FontSize="16" />
<Border BorderBrush="LightGray" BorderThickness="1" Width="300" Margin="5" HorizontalAlignment="Left">
<StackPanel DataContext="{StaticResource SampleDate}">
<TextBlock TextWrapping="Wrap" Text="StringFormat=f" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=f}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=F" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=F}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=g" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=g}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=G" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=G}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=m" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=m}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=y" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=y}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=t" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=t}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=T" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=T}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=u" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=u}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=o" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=o}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=MM/dd/yy" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=MM/dd/yy}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=MMMM dd\, yyyy g" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=MMMM dd\, yyyy g}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=hh:mm:ss.fff tt" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=hh:mm:ss.fff tt}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat=\{0:r\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat=\{0:r\}}" Margin="8,0" />
<TextBlock TextWrapping="Wrap" Text="StringFormat='MMM d, yyyy'" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock TextWrapping="Wrap" Text="{Binding CurrentDateTime, StringFormat='MMM d, yyyy'}" Margin="8,0" />
</StackPanel>
</Border> 
</StackPanel>
</StackPanel>
<StackPanel Orientation="Vertical" >
<TextBlock Text="StringFormat和数字演示" Margin="8" FontSize="16"/>
<Border BorderBrush="LightGray" BorderThickness="1" Width="300" Margin="5" HorizontalAlignment="Left">
<StackPanel Orientation="Vertical" Margin="5">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="18"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto" MinWidth="28"/>
</Grid.ColumnDefinitions>
<TextBlock Text="1" HorizontalAlignment="Left" d:LayoutOverrides="Height" />
<TextBlock Text="100,000" d:LayoutOverrides="Width, Height" Grid.Column="2" Margin="0" />
<Slider x:Name="BigNumSlider" Margin="0" d:LayoutOverrides="Height" Grid.ColumnSpan="3" Grid.Row="1" Maximum="100000" Minimum="1" Value="50000"/>
</Grid>
<TextBlock TextWrapping="Wrap" Text="StringFormat=c" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider, Mode=TwoWay, StringFormat=c}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=目前拥有现金 \{0:C0\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider, Mode=TwoWay, StringFormat=目前拥有现金 \{0:C0\}}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=e" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider, Mode=TwoWay, StringFormat=e}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=\{0:E4\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider, Mode=TwoWay, StringFormat=\{0:E4\}}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=n" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider, Mode=TwoWay, StringFormat=n}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=\{0:N0\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider, Mode=TwoWay, StringFormat=\{0:N0\}}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat='###,###.##'" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider, Mode=TwoWay, StringFormat='###,###.##'}" Margin="8,0" />
</StackPanel>
</Border>
<Border BorderBrush="LightGray" BorderThickness="1" Width="300" Margin="5" HorizontalAlignment="Left">
<StackPanel Orientation="Vertical" Margin="5">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="18"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto" MinWidth="28"/>
</Grid.ColumnDefinitions>
<TextBlock Text="-1" HorizontalAlignment="Left" d:LayoutOverrides="Height" />
<TextBlock Text="1" d:LayoutOverrides="Width, Height" Grid.Column="2" Margin="0" />
<Slider x:Name="BigNumSlider1" Margin="0" d:LayoutOverrides="Height" Grid.ColumnSpan="3" Grid.Row="1" Maximum="1" Minimum="-1" Value="0.2" LargeChange="0.1" SmallChange="0.01"/>
</Grid>
<TextBlock TextWrapping="Wrap" Text="StringFormat=n" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider1, Mode=TwoWay, StringFormat=n}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=\{0:N4\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider1, Mode=TwoWay, StringFormat=\{0:N4\}}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=p" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider1, Mode=TwoWay, StringFormat=p}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=\{0:P0\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider1, Mode=TwoWay, StringFormat=\{0:P0\}}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=\{0:E5\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider1, Mode=TwoWay, StringFormat=\{0:E5\}}" Margin="8,0"/>
<TextBlock TextWrapping="Wrap" Text="StringFormat=\{0:C3\}" FontWeight="Bold" Margin="0,4,0,2"/>
<TextBox TextWrapping="Wrap" Text="{Binding Value, ElementName=BigNumSlider1, Mode=TwoWay, StringFormat=\{0:C3\}}" Margin="8,0"/>
</StackPanel>
</Border>
</StackPanel>

</StackPanel>
</ScrollViewer>
</Grid>