wpf - Adding Text inside a Shape -


i have custom wpf control myline should represent or not text in middle.

public class myline : shape {        public double x1, y1, x2, y2;     public bool istextdisplayed;     public string caption;      protected override system.windows.media.geometry defininggeometry     {                 {             var geometrygroup = new geometrygroup();              if (istextdisplayed)             {                 // calculate text point                 var midpoint = new point((x1 + x2) / 2.0, (y1 + y2) / 2.0);                 // add textblock caption text in point                 // ???             }              // add line             geometrygroup.children.add(new linegeometry(                 new point(x1, y1), new point(x2, y2)));              return geometrygroup;          }     } } 

how should add textblock (or label) here?

i tried add formattedtext inside, nok, because draws text line fat brush , impossible read something.

edit

adding visual child

public myline() : base() {     textblock = new system.windows.controls.textblock();     textblock.visibility = system.windows.visibility.hidden;     this.addvisualchild(textblock); }  protected override system.windows.media.geometry defininggeometry {         {     ...  if (istextdisplayed) {     var midpoint = new point((x1 + x2) / 2.0, (y1 + y2) / 2.0);     string text = "some custom text";      canvas.setleft(textblock, midpoint.x);     canvas.setbottom(textblock, midpoint.y);      textblock.text = text;     this.textblock.visibility = system.windows.visibility.visible;     } else  {     this.textblock.visibility = system.windows.visibility.hidden; } 

i don't see label... :"/

edit2

adding adorner

public myline() : base() {     this.loaded += new routedeventhandler(myline_loaded); }  void myline_loaded(object sender, routedeventargs e) {     adornerlayer alayer = adornerlayer.getadornerlayer(this);     if (alayer != null)         alayer.add(new textadorner(this));         }  class textadorner : adorner {     public textadorner(uielement adornedelement) : base(adornedelement)      { }      protected override void onrender(drawingcontext drawingcontext)     {         myline segment = (this.adornedelement myline);                     if (segment != null && segment.islabelused)         {             rect segmentbounds = new rect(segment.desiredsize);              formattedtext ft = new formattedtext(                 "654 m", thread.currentthread.currentculture,                 system.windows.flowdirection.lefttoright,                 new typeface("arial"), 12, brushes.white);              drawingcontext.drawtext(ft, segmentbounds.bottomright);         }     } } 

now, apparently code never enters in onrender adorner method...

if don't want text displayed in same brush line, don't want use shape base class, geometry returned defininggeometry rendered in 1 brush. if want use shape base class, want add text visual child rather add existing geometry.

if aren't going using lot of these, , can afford heavy-weight container, i'd suggest creating usercontrol based control contains shape created, and text element textbox on canvas (for absolute positioning). again, not going great solution if have hundreds or thousands of these, if have tens of them, easiest , quickest solution.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -