ReoGrid Forum

Fast and powerful .NET Spreadsheet Component

You are not logged in.

Announcement

This forum has been archived and no longer accepts new user registrations. Please report your questions, problems, and feedback to the issue page of ReoGrid on GitHub. Thank you for your cooperation.

https://github.com/unvell/ReoGrid/issues

#1 2017-03-30 14:46:03

sirmfletcher
Member
Registered: 2016-12-10
Posts: 8

How to fix WPF Text Alignment for Cells

All,

Discovered issues, with trying to display text in cells that should be centered that wraps more than 1 line, and also with right/left  aligned text.
Filename to edit:   ReoGrid\WPF\Renderer.cs

The problem found was that formattedText property(MSDN lookup FormattedText object) of WPF cells didn't have text alignment set, and location to draw at was wrong! Digging deeper, lead to some interesting results.

Fix Steps are
1) Open the above filename
2) Locate the section for Renderer, function  DrawCellText  as show below, around line#550 or so....

 #region Renderer

    internal class WPFRenderer : WPFGraphics, IRenderer
	{
		protected System.Windows.Media.Typeface headerTextTypeface;

		internal WPFRenderer()
		{
			this.headerTextTypeface = unvell.ReoGrid.Rendering.PlatformUtility.GetFontDefaultTypeface(System.Windows.SystemFonts.SmallCaptionFontFamily);
		}

public void DrawCellText(Cell cell, SolidColor textColor, DrawMode drawMode, double scale)
{             var sheet = cell.Worksheet;

			if (sheet == null) return;


            if (cell.formattedText == null)
            {
                sheet.UpdateCellFont(cell);
            }
         
          this.PlatformGraphics.DrawText(cell.formattedText, cell.TextBounds.Location);
}

Step 3...  Comment out the last line above in the function
//this.PlatformGraphics.DrawText(cell.formattedText, cell.TextBounds.Location);

Step 4.  Insert after the if(cell.formattted.Text == null) statement the below code!

            // Added by M. Fletcher 3/30/2017 
            // grab the cell style and set text alignment
            switch (cell.Style.HAlign)
            {
                case ReoGridHorAlign.Center:
                    cell.formattedText.TextAlignment = TextAlignment.Center;
                    break;
                case ReoGridHorAlign.Right:
                    cell.formattedText.TextAlignment = TextAlignment.Right;
                    break;
                case ReoGridHorAlign.Left:
                    cell.formattedText.TextAlignment = TextAlignment.Left;
                    break;
                case ReoGridHorAlign.DistributedIndent:
                    cell.formattedText.TextAlignment = TextAlignment.Justify;
                    break;
            }

            // give renderer proper coordinates to draw the text given textalignment
            if (cell.formattedText.TextAlignment == TextAlignment.Center)
            {
                double x;
                double y;
                x = cell.Bounds.Left;

                // without this we get badly aligned text!!this was hardest to findout affects the text renderer greatly!!!
                if (cell.formattedText.MaxTextWidth == 0)
                    cell.formattedText.MaxTextWidth = cell.Bounds.Width;


                y = (cell.TextBoundsHeight - cell.formattedText.Height) / 2 + cell.TextBoundsTop;
                
                this.PlatformGraphics.DrawText(cell.formattedText, new Point(x, y));
                
            }
            else if (cell.formattedText.TextAlignment == TextAlignment.Left)
            {
                this.PlatformGraphics.DrawText(cell.formattedText, new Point(cell.TextBounds.Left, cell.Bounds.Top));
            }
            else if (cell.formattedText.TextAlignment == TextAlignment.Right)
            {
                this.PlatformGraphics.DrawText(cell.formattedText,
              new Point(cell.TextBoundsLeft + cell.TextBounds.Width, cell.Bounds.Top));
                // cell.Bounds.Left +cell.Bounds.Width
            }
            else  // didn't handle Justify or General values , so use default method instead will need a fix if you use either alignment method
            {
                this.PlatformGraphics.DrawText(cell.formattedText, cell.TextBounds.Location);
            }
            //this.PlatformGraphics.DrawText(cell.formattedText, cell.TextBounds.Location);

It should work properly after that!   
It took many trace statements and MSDN info on FormattedText to get to the bottom of this issue!

Have Fun,
Fletch

Offline

#2 2017-03-30 17:31:35

Jingwood
Moderator
From: jing at reogrid.net
Registered: 2014-06-03
Posts: 615

Re: How to fix WPF Text Alignment for Cells

Nice! Thanks!

Offline

Board footer

Powered by FluxBB