Thomas
Thomas

Reputation: 3358

Add ellipses in text

I've got a Label with a user-selected directory path. Of course some paths are longer than others. I'm using a Resizer on the control the Label lives in, and would love it if I could have variable eliding of the path.

c:\very\long\path\to\a\filename.txt collapsing to c:...\filename.txt or c:\very...\filename.txt. You get the picture - bigger window gives more info, shrink it down and you still get the important parts of the path. I'd love it if I didn't have to have a custom control, but I can live with it.

Custom Text Wrapping in WPF seems like it might do the job, but I'm hoping for something simpler.

EDIT Sorry, I meant to convey that I want the eliding to vary based on width of the Label.

Upvotes: 1

Views: 898

Answers (1)

Artur Carvalho
Artur Carvalho

Reputation: 7167

That example you gave is for non-rectangular containers. If you don't need that you can use a Value Converter. If its bigger than the label, you put ellipses:

Not tested example:

class EllipsisConverter : IValueConverter
{
    #region IValueConverter Members

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

if (path.Length > 100)
{
return path.Substring(0, 100) + "...";   
}else{
   return path;
}
    }

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

    #endregion
}

Upvotes: 1

Related Questions