Silverlight 4常用StringFormat格式总结
在Silverlight项目中,经常会根据需求的不同,提供不同的字符格式到客户端。这篇文章总结了目前Silverlight 4常用StringFormat绑定字符格式,方便大家在随后的开发中调用参考。
在Silverlight项目中,经常会根据需求的不同,提供不同的字符格式到客户端。这篇文章总结了目前Silverlight 4常用StringFormat绑定字符格式,方便大家在随后的开发中调用参考。
早期的格式转换方法
在Silverlight 4之前的版本,通常修改字符格式是通过实现IValueConverter接口,在IValueConverter接口中包含有两个方法:
1. Convert,在绑定源数据传送到客户端显示前,转换修改数据;
2. ConvertBack,在目标数据传送到绑定源数据前,转换修改数据,该方法仅被用于“TwoWay”绑定模式;
例如,根据布尔型判断实现控件是否显示在客户端,下面是一个简单的转换代码,通过实现IValueConverter,判断控件Visibility属性值。
public
class
VisibilityConverter : IValueConverter
{
public
object
Convert(object
value,Type targetType,object
parameter,CultureInfo culture)
{
bool
visibility = (bool)value;
return
visibility ? Visibility.Visible : Visibility.Collapsed;
}
public
object
ConvertBack(object
value,Type targetType,object
parameter,CultureInfo culture)
{
Visibility visibility = (Visibility)value;
return
(visibility == Visibility.Visible);
}
}
Silverlight 4的StringFormat属性
在Silverlight 4中,数据绑定SDK提供一个“StringFormat”的属性,该属性允许开发人员自由定义输出字符格式,例如,DataPicker控件,该控件提供SelectedDateFormat属性,控制其输出格式“短格式”或“长格式”。
SelectedDateFormat="Short", “默认”
SelectedDateFormat="Long"
而如果需要输出“April 01, 2011”格式,则无法使用SelectedDateFormat控制其格式,这时可以使用StringFormat属性完成需求。
<toolkit:DatePicker SelectedDate="{Binding DemoDate,Mode=TwoWay, StringFormat='MMM d, yyyy'}" />
再例如,早期实现一段文字绑定信息需要按照以下格式操作:
<TextBlock Text=”当前登录用户: ”/>
<TextBlock Text=“{Binding Name}”/>
输出: 当前登录用户Jv9
在Silverlight 4中使用StringFormat属性,可以简单的实现如下:
<TextBlock Text=“{Binding Name, StringFormat=’当前登录用户: \{0\}’}”>
这样实现简单而又方便。
对于StringFormat属性的使用方法还有很多,下面使用实例演示常用的属性:
<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='正在输入 "\{0\}"." FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock Text="{Binding Text, ElementName=txtSource, StringFormat='正在输入 "\{0\}".'}" 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>
<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='正在输入 "\{0\}"." FontWeight="Bold" Margin="0,4,0,2"/>
<TextBlock Text="{Binding Text, ElementName=txtSource, StringFormat='正在输入 "\{0\}".'}" 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>
<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>