ConvtoBSfcodeToStr.cs
namespace SilverlightApplication1 { public class ConvtoBSfcodeToStr : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return (value.ToString().Length < 20 ? value.ToString() : value.ToString().Substring(0,20)); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return (value.ToString().Length < 20 ? value.ToString() : value.ToString().Substring(0, 20)+"..."); } } }
xaml:
xmlns:localConv="clr-namespace:SilverlightApplication1" <localConv:ConvtoBSfcodeToStr x:Key="ConverBStoStr"></localConv:ConvtoBSfcodeToStr>
使用时 先转换为模板列 Converter={StaticResource ConverBStoStr}
<data:DataGridTemplateColumn Header="内容" Width="1.0*"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Name="tool" Text="{Binding ArticleContent, Mode=TwoWay, Converter={StaticResource ConverBStoStr}}"> <ToolTipService.ToolTip> <ToolTip MaxWidth="500"> <TextBlock Text="{Binding ArticleContent}" TextWrapping="Wrap"/> </ToolTip> </ToolTipService.ToolTip> </TextBlock> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn>