I'd like my wpf textbox to have a minimum width large enough to accomodate exactly 1 character as it would be drawn depending on the font family, font size, and other properties of the wpf text(box) AND the current culture (could be any). Can I just set the MinWidth property to the FontSize property to achieve this? That's probably too simple to work..
Asked
Active
Viewed 2,969 times
1 Answers
2
You can use TextRenderer.MeasureText(string, font).
Here is an example:
TextBox textBox = new TextBox();
Size size = TextRenderer.MeasureText("A", textBox.Font);
textBox.Width = size.Width;

SwDevMan81
- 48,814
- 22
- 151
- 184
-
but TextRenderer is in the Forms namespace, is it also usable for my wpf app/textbox (see tag)? – mtijn Sep 02 '11 at 13:14
-
2@mtijn - You should be able to use this (http://stackoverflow.com/questions/824281/wpf-equivalent-to-textrenderer) – SwDevMan81 Sep 02 '11 at 13:22
-
those methods calculate font width for a piece of text, but I cannot know in advance which character of a font, in say an unknown arabic or asian culture, will require the largest possible space that I should calculate.. – mtijn Sep 02 '11 at 13:57
-
combined with the comment this answer is definitely useful, except I would rather have known the maximum needed size to accomodate the biggest character for the current font and culture (instead of relying on some piece of text input). in absence of better answers I'll mark this one even though it doesn't fully match my question. – mtijn Sep 12 '11 at 09:54