TextBlock with ellipsis at left or center
If you want a TextBlock with ellipsis a Left, Center or Right use this
you must use TrimmedText in place of Text for binding original text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
[Localizability(LocalizationCategory.None, Readability = Readability.Unreadable)] public enum EllipsisPosition { // // Résumé : // Par défaut. elispsis sur la droite. Right = 0, // // Résumé : // elispsis sur la gauche Left = 1, // // Résumé : // elispsis an centre. Center = 2, } public class TextBlockTrim : TextBlock { private const string ELLIPSIS = "..."; #region static static TextBlockTrim() { TextAlignmentProperty.OverrideMetadata(typeof(TextBlockTrim), new FrameworkPropertyMetadata( TextAlignment.Left, FrameworkPropertyMetadataOptions.Inherits, (sender, e) => ((TextBlockTrim)sender)._Changed(e))); VisibilityProperty.OverrideMetadata(typeof(TextBlockTrim), new FrameworkPropertyMetadata( Visibility.Visible, FrameworkPropertyMetadataOptions.Inherits, (sender, e) => ((TextBlockTrim)sender)._visibility(e))); } #endregion public TextBlockTrim() { this.SizeChanged += (ss, ee) => UpdateText(); this.TextTrimming = TextTrimming.CharacterEllipsis; } #region TextAlignmentChanged private bool protect = false; private void _Changed(DependencyPropertyChangedEventArgs e) { if (protect) return; switch (e.NewValue) { case TextAlignment ta: previousTextAligment = ta; UpdateText(); break; case string s: UpdateText(s); break; case EllipsisPosition ep: UpdateText(); break; default: throw new Exception("Unknow type"); } } #endregion #region VisibilityChanged private void _visibility(DependencyPropertyChangedEventArgs e) { UpdateText(); } #endregion #region EllipsisPosition public EllipsisPosition EllipsisPosition { get { return (EllipsisPosition)GetValue(EllipsisPositionProperty); } set { SetValue(EllipsisPositionProperty, value); } } // Using a DependencyProperty as the backing store for EllipsisPosition. This enables animation, styling, binding, etc... public static readonly DependencyProperty EllipsisPositionProperty = DependencyProperty.Register("EllipsisPosition", typeof(EllipsisPosition), typeof(TextBlockTrim), new FrameworkPropertyMetadata(EllipsisPosition.Right, (sender, e) => ((TextBlockTrim)sender)._Changed(e))); #endregion #region TrimmedText (DP SHORT) public string TrimmedText { get { return (string)GetValue(TrimmedTextProperty); } set { SetValue(TrimmedTextProperty, value); } } public static readonly DependencyProperty TrimmedTextProperty = DependencyProperty.Register( "TrimmedText", typeof(string), typeof(TextBlockTrim), new FrameworkPropertyMetadata(null, (sender, e) => ((TextBlockTrim)sender)._Changed(e))); #endregion #region UpdateText string previousText = null; TextAlignment previousTextAligment = TextAlignment.Left; private void UpdateText(string newText = null) { if (newText != null) previousText = newText; if (previousText == null) { Text = null; return; } if (this.ActualWidth == 0) return; try { string str = previousText; if (!string.IsNullOrEmpty(previousText)) { double w = MeasureText(previousText, this).Width; double width = ActualWidth; if (w > width && width != 0) { ToolTip = previousText; switch (EllipsisPosition) { case EllipsisPosition.Right: { do { str = str.Substring(1); w = MeasureText(ELLIPSIS + str, this).Width; } while (w > width); str = ELLIPSIS + str; protect = true; TextAlignment = TextAlignment.Left; protect = false; } break; case EllipsisPosition.Left: { int size = str.Length; do { str = str.Substring(0, size); w = MeasureText(str + ELLIPSIS, this).Width; size--; } while (w > width); str = str + ELLIPSIS; protect = true; TextAlignment = TextAlignment.Right; protect = false; } break; case EllipsisPosition.Center: { string l, r, t; { int size = str.Length / 2; int pos = size; do { l = str.Substring(0, size); r = str.Substring(pos); t = l + ELLIPSIS + r; w = MeasureText(t, this).Width; size--; pos++; } while (w > width); str = t; } protect = true; TextAlignment = TextAlignment.Center; protect = false; } break; } } else { ToolTip = null; protect = true; TextAlignment = previousTextAligment; protect = false; } } Text = str; } catch { Text = null; } } protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) { base.OnRenderSizeChanged(sizeInfo); UpdateText(); } #endregion #region Static functions public static Size MeasureText(string text, TextBlock textBlock) { return MeasureText(text, textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch, textBlock.FontSize, textBlock); } public static Size MeasureText(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize, Visual reference) { Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch); GlyphTypeface glyphTypeface; if (!typeface.TryGetGlyphTypeface(out glyphTypeface)) { return MeasureTextSize(text, fontFamily, fontStyle, fontWeight, fontStretch, fontSize, reference); } double totalWidth = 0; double height = 0; for (int n = 0; n < text.Length; n++) { ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]]; double width = glyphTypeface.AdvanceWidths[glyphIndex] * fontSize; double glyphHeight = glyphTypeface.AdvanceHeights[glyphIndex] * fontSize; if (glyphHeight > height) { height = glyphHeight; } totalWidth += width; } return new Size(totalWidth, height); } public static Size MeasureTextSize(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize, Visual reference) { FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(fontFamily, fontStyle, fontWeight, fontStretch), fontSize, Brushes.Black, VisualTreeHelper.GetDpi(reference).PixelsPerDip); return new Size(ft.Width, ft.Height); } #endregion } |
and use like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<Grid> <TextBlock HorizontalAlignment="Left" Margin="10,18,0,0" TextWrapping="Wrap" Text="elipsis left" FontWeight="Bold" VerticalAlignment="Top" Width="118" /> <TextBlockTrim Background="Aqua" TrimmedText="123456789 123456789 123456789 123456789" Margin="10,39,9.6,0" EllipsisPosition="Left" TextWrapping="Wrap" VerticalAlignment="Top" Visibility="Visible" /> <TextBlockTrim Background="Aqua" TrimmedText="123456789 123456789 123456789 123456789" TextAlignment="Center" EllipsisPosition="Left" Margin="10,60,9.6,0" TextWrapping="Wrap" VerticalAlignment="Top" Visibility="Visible" /> <TextBlockTrim Background="Aqua" TrimmedText="123456789 123456789 123456789 123456789" Margin="10,81,9.6,0" TextAlignment="Right" EllipsisPosition="Left" TextWrapping="Wrap" VerticalAlignment="Top" Visibility="Visible" /> <TextBlock HorizontalAlignment="Left" Margin="10,108,0,0" TextWrapping="Wrap" Text="elipsis right" FontWeight="Bold" VerticalAlignment="Top" Width="118" /> <TextBlockTrim Background="Aqua" TrimmedText="123456789 123456789 123456789 123456789" Margin="10,130,9.6,0" TextWrapping="Wrap" VerticalAlignment="Top" Visibility="Visible" /> <TextBlockTrim Background="Aqua" TrimmedText="123456789 123456789 123456789 123456789" TextAlignment="Center" Margin="10,151,9.6,0" TextWrapping="Wrap" VerticalAlignment="Top" Visibility="Visible" /> <TextBlockTrim Background="Aqua" TrimmedText="123456789 123456789 123456789 123456789" Margin="10,172,9.6,0" TextAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" Visibility="Visible" /> <TextBlock HorizontalAlignment="Left" Margin="10,204,0,0" TextWrapping="Wrap" Text="elipsis center" FontWeight="Bold" VerticalAlignment="Top" Width="118" /> <TextBlockTrim Background="Aqua" TrimmedText="123456789 123456789 123456789 123456789" Margin="10,226,9.6,0" EllipsisPosition="Center" TextWrapping="Wrap" VerticalAlignment="Top" /> <TextBlockTrim Background="Aqua" TrimmedText="123456789 123456789 123456789 123456789" EllipsisPosition="Center" TextAlignment="Center" Margin="10,247,9.6,0" TextWrapping="Wrap" VerticalAlignment="Top" Visibility="Visible" /> <TextBlockTrim Background="Aqua" TrimmedText="123456789 123456789 123456789 123456789" Margin="10,268,9.6,0" EllipsisPosition="Center" TextAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" Visibility="Visible" /> </Grid> |
see result
Update to v2 :
refresh when visibility changed
refresh when size changed
Categories: Non classé
Commentaires récents