4

I need to do a special text trimming. Lets say that my string is: abcd

The default trimming would give me this: ab...

But I need it to be. a..d

Any idea how to implement it?

currnently I'm using

<TextBlock Text="abcdLongWord" TextTrimming="CharacterEllipsis"/>
Erez
  • 6,405
  • 14
  • 70
  • 124
  • What is the criteria for the text to be trimmed? You want only the first or last character? – CodeZombie Apr 09 '13 at 11:20
  • ZombieHunter - I would like that the number of characters that are displayed would be configurable, but for simplifying we can start with only one character – Erez Apr 09 '13 at 11:23
  • http://stackoverflow.com/q/1041820/413032 – Davut Gürbüz Apr 09 '13 at 11:23
  • 2
    I found such a function: http://vt-studio.co.uk/wp/?p=32 – Arvo Apr 09 '13 at 11:34
  • What is the purpose of the XML in your question? The string you want to trim is just a simple string, right? As in "_show only first and last letter if the string is longer than X_", am I correct? – Kjartan Apr 09 '13 at 11:41
  • Arvo - it seems like you are the winner, plaese make an asnwer and I will accept it. – Erez Apr 09 '13 at 12:31

2 Answers2

1

I have had this concern in the past and wrote my own converter to handle the process of Kearning.

Converter Class

internal class KearningConverter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        string result = value.ToString();

        try {
            int length = int.Parse(parameter.ToString());

            if (result.Length > length) {
                result = result.Substring(0, length) + "...";
            }
        } catch {
            result += "...";
        }
        return result;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        throw new NotImplementedException();
    }
}

Xaml Markup

xmlns:conv="clr-namespace:project.converters;assembly=project"

<Window.Resources>
<conv:KearningConverter x:Key="kearnConverter"/>
</Window.Resources>

<TextBlock Text="{Binding Path=AttributeName, Converter={StaticResource kearnConverter}, ConverterParameter=3}"/>

This way you can have multiple implementations of the kearning based on different requirements depening on the layout of your UI.

sepai
  • 146
  • 5
  • And now the funny part is to calculate ConverterParameter value dynamically. And what if TextBlock size is changed? We should trim in this case too, isn\`t it? – icebat Apr 09 '13 at 13:04
-3

Not tested this but depending on what you actually need:

string test = "abcdefghij";
  int nCharNum = test.Length();
  test.Remove(2, nCharNum - 1); // this will get you "aj"
  //or
  test.Remove(2, 3); // this will result in "adefg..."
  //or
  test.Remove(2, nCharNum - 1);
  test.Insert(2, "..") // this will get you "a..j"

Hope this helps. (Indexes may need some correction)

Łukasz Motyczka
  • 1,169
  • 2
  • 13
  • 35
  • Thanks, but what you suggested is not textTrimming it is string manipulation, I need the text to trimm depends on the ui element width and not by hardcoded decision. – Erez Apr 09 '13 at 11:30
  • Do you want to replace inside characters when some textox is smaller then some value? Is that right? – Łukasz Motyczka Apr 09 '13 at 11:42
  • @Erez: Where is that specified in your original question? You can't expect a "correct" answer with an imprecise question, and an answer certainly should not be voted down if it does in fact answer the written question. – Kjartan Apr 09 '13 at 12:43
  • I think the question is very precise. – Matthias Sep 22 '16 at 22:47