How WPF layout deals with text wrapping -


wpf layout works first asking controls measure themselves, , arranges controls based on desired size produced during measure stage.

i not see how possible deal text wrapping in case.

a control contains text wrapped must know width before can calculate height. @ measure stage width unknown. assigned arrange stage, when measuring completed. height cannot calculated during measure.

how text wrapping work in wpf?

this simple custom textblock shows how works:

public class mytextblock : frameworkelement {     private formattedtext formattedtext;      public static readonly dependencyproperty textproperty =         dependencyproperty.register(             "text", typeof(string), typeof(mytextblock),             new frameworkpropertymetadata(                 null,                 frameworkpropertymetadataoptions.affectsmeasure,                 (o, e) =>                 {                     ((mytextblock)o).formattedtext = new formattedtext(                         (string)e.newvalue,                         cultureinfo.invariantculture,                         flowdirection.lefttoright,                         new typeface("segoe ui"),                         24,                         brushes.black);                 }));      public string text     {         { return (string)getvalue(textproperty); }         set { setvalue(textproperty, value); }     }      protected override size measureoverride(size availablesize)     {         var size = new size();          if (formattedtext != null)         {             formattedtext.maxtextwidth = availablesize.width;             size.width = formattedtext.width;             size.height = math.min(formattedtext.height, availablesize.height);         }          return size;     }      protected override size arrangeoverride(size finalsize)     {         if (formattedtext != null)         {             formattedtext.maxtextwidth = finalsize.width;             formattedtext.maxtextheight = finalsize.height;         }          return finalsize;     }      protected override void onrender(drawingcontext drawingcontext)     {         base.onrender(drawingcontext);          if (formattedtext != null)         {             drawingcontext.drawtext(formattedtext, new point());         }     } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -