Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] GridView HeaderText alignment #819

Closed
msasso69 opened this issue Jun 7, 2017 · 4 comments · Fixed by #1881
Closed

[Feature] GridView HeaderText alignment #819

msasso69 opened this issue Jun 7, 2017 · 4 comments · Fixed by #1881
Milestone

Comments

@msasso69
Copy link
Contributor

msasso69 commented Jun 7, 2017

Hi All,

I've not being able to set the Height of a GridView header or have the HeaderText wrapped across multiple lines.

Basically what I would like to have is something like this:
image

But what I've been able to do so far with Eto.GridView is only this:
image

Basically if I set the width of a column the Header gets truncated and there's no way to have it spanned on multiple lines on a thicker header.

Is this somehow already possible or it's a missing feature?

Further it seems to me that the HeaderText is Always aligned on the left and it is not possible to align it differently. Am I wrong?
image

Thanks

@msasso69
Copy link
Contributor Author

msasso69 commented Jun 8, 2017

I managed to span the header text on multiple lines adding "new line" characters at the end of each line (unfortunately works only on Wpf, not on Mac, I have no clue on Linux with Gtk and on Windows with Winforms) and adding proper spaces at the beginning of each line in order to align each line at the center.

Of course it would be nice if the control did it automatically.

Anyway the following snippet of code does the trick on Wpf:

	{
		// apply resource to texts

		string text = GetHeaderText();
		this.Text = text;

		// the header of each column needs to be spanned on multiple columns and aligned at the center

		int length = (int)SystemFonts.Menu().MeasureString(text).Width;
		StringBuilder newText = new StringBuilder();
		string filler;
		int padding = 10; // 5 pixels per side

		if (length + padding > column.Width)
		{
			string[] tokens = text.Split(' ');
			StringBuilder line = new StringBuilder(tokens[0]);
			int lineLength = (int)SystemFonts.Menu().MeasureString(line.ToString()).Width;
			for (int i = 0; i < tokens.Length - 1; i++)
			{
				string tmpLine = string.Format("{0} {1}", line, tokens[i + 1]);
				int tmpLineLength = (int)SystemFonts.Menu().MeasureString(tmpLine).Width;
				if (tmpLineLength + padding > column.Width)
				{
					// add as many spaces to the left in order to align the text at the center
					filler = GetFiller(column.Width, lineLength, padding);
					newText.Append(filler);

					// add now the text
					newText.AppendFormat("{0}\n", line);

					// reset the line
					line.Clear();
					line.Append(tokens[i + 1]);
					lineLength = (int)SystemFonts.Menu().MeasureString(line.ToString()).Width;	
				}
				else
				{
					line.AppendFormat(" {0}", tokens[i + 1]);
					lineLength = (int)SystemFonts.Menu().MeasureString(line.ToString()).Width;
				}
			}

			// add as many spaces to the left in order to align the text at the center
			filler = GetFiller(column.Width, lineLength, padding);
			newText.Append(filler);

			// add now the text
			newText.Append(line);

			text = newText.ToString();
		}
		else
		{
			// add as many spaces to the left in order to align the text at the center
			filler = GetFiller(column.Width, length, padding);
			newText.Append(filler);

			// add now the text
			newText.Append(text);

			text = newText.ToString();
		}
		
		column.HeaderText = text;
	}

	string GetFiller(int columnWidth, int textLength, int padding)
	{
		int leftAlignement = (columnWidth - textLength - padding) / 2;
		StringBuilder tmpFiller = new StringBuilder();
		string filler = tmpFiller.ToString();
		while (leftAlignement > (int)SystemFonts.Menu().MeasureString(tmpFiller.ToString()).Width)
		{
			filler = tmpFiller.ToString();
			tmpFiller.Append(" ");
		}
		return filler;
	}

@cwensley cwensley changed the title [Feature] GridView Header height (or Header word wrap) and HeaderText alignement [Feature] GridView HeaderText alignment Jun 9, 2017
@cwensley
Copy link
Member

cwensley commented Jun 9, 2017

Thanks for the report! Adding alignment options for the header text is definitely needed. As for adding newlines or having multiline headers I'm not sure if that can be supported on all platforms. It'd be worth testing though!

@msasso69
Copy link
Contributor Author

Thanks for working on it, I'll give it a try asap!

@seghier
Copy link

seghier commented Mar 2, 2022

Hello, any idea how to change font and size of the header text?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants