有时候会遇到这种情况,用了数据绑定显示的数据太长时,如何让过长的数据显示规定的长度,多余的用省略号代替呢,自己写了个简单的小例子和大家分享一下^_^,我也是学习WPF不久,这是我第一次写博客,有问题还希望大家指出。

先看看最终的效果吧

WPF xmal绑定数据,当显示数据过长用省略号代替的方法

实现方式自定义一个StringformatConvert类,实现IValueConverter接口,绑定的时候加上转化器就行了。

 1 public class StringformatConvert:IValueConverter
 2     {
 3         public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 4         {
 5             string s = value.ToString();
 6             int leng;
 7             if (int.TryParse(parameter.ToString(), out leng))
 8             {
 9                 if (s.Length <= leng)
10                     return s;
11                 else
12                     return s.Substring(0, leng) + "...";
13             }
14             else
15                 return string.Empty;
16         }
17 
18         public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
19         {
20             throw new NotImplementedException();
21         }
22     }
View Code

相关文章:

  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-07
  • 2022-02-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案