Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
TrimString.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Globalization;
5 
6 namespace SiliconStudio.Presentation.ValueConverters
7 {
8  /// <summary>
9  /// This converter will trim the string representation of an object to the given number of characters, adding "..." at the end of the resulting string.
10  /// The number of character must be passed via the converter parameter.
11  /// </summary>
12  /// <remarks>If the parameter is a negative number, its absolute value will be used and no trailing "..." will be added.</remarks>
13  public class TrimString : OneWayValueConverter<TrimString>
14  {
15  /// <inheritdoc/>
16  public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
17  {
18  // We get the length first to always throw an exception if the parameter in incorrect
19  int length;
20  try
21  {
22  length = System.Convert.ToInt32(parameter);
23  if (length == 0) throw new Exception();
24  }
25  catch (Exception)
26  {
27  throw new FormatException("The parameter must be convertible to a non-null integer.");
28  }
29 
30  if (value == null)
31  return null;
32 
33  bool addEllipsis = length >= 0;
34  length = Math.Abs(length);
35  var str = value.ToString();
36 
37  return str.Length > length ? str.Substring(0, length) + (addEllipsis ? "..." : "") : str;
38  }
39  }
40 }
override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
Definition: TrimString.cs:16
This converter will trim the string representation of an object to the given number of characters...
Definition: TrimString.cs:13