locked
How to get ellipsis in the middle of the text?

    Question

  • Hi 

    I want to get the word ellipsis in the middle of a long text.

    The texttrimming property - WordEllipsis and CharacterEllipsis add the ellipsis at the end and not in the middle.

    Ex: I want - PocketTanks...Edition2013  instead of PocketTanksGamePlayer...

    How can i achieve this?

    Thanks

    Tuesday, September 02, 2014 9:07 AM

Answers

  • You need to trim the Text yourself. You could bind to a property containing the long text and use a converter to trim down the text:

    public class CustomConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                string text = value as string;
                if (text.Length > 15)
                {
                    return string.Format("{0}...{1}", text.Substring(0,11), text.Substring(text.Length - 11));
                }
    
                return value;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotSupportedException();
            }
        }
    

    public sealed partial class MainPage : Page
    {
    
    
          public string LongText
            {
                get 
                {
                    return "PocketTanksGamePlayerEdition2013";
                }
            }
    
            public MainPage()
            {
    
                this.InitializeComponent();
     }
    }
    

    <Page
        x:Class="App3.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App3"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" Name="page">
        <Page.Resources>
                  <local:CustomConverter x:Key="converter"/>
        </Page.Resources>
    
    
        <StackPanel>
     <TextBlock Text="{Binding Path=LongText, ElementName=page, Converter={StaticResource converter}}" />
     ...
    

    Please refer to the following page for more information about how to implement and use a converter: http://msdn.microsoft.com/en-us/library/windows.ui.xaml.data.ivalueconverter.Aspx

    • Proposed as answer by St4mos Tuesday, September 02, 2014 5:15 PM
    • Marked as answer by VDNS Tuesday, September 09, 2014 4:57 AM
    Tuesday, September 02, 2014 1:45 PM