tags:

views:

176

answers:

1
+1  Q: 

Elide 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.

http://stackoverflow.com/questions/108689/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.

+1  A: 

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
}
Artur Carvalho