<TextBox Height="?" Width="?" AcceptReturn="true" />
Height = Random; 寬度=隨機; 例如,高度爲60,寬度爲100 如何控制文本inputed lentgh比文本框的大小更小wpf,如何限制TextBox的MaxLength?
<TextBox Height="?" Width="?" AcceptReturn="true" />
Height = Random; 寬度=隨機; 例如,高度爲60,寬度爲100 如何控制文本inputed lentgh比文本框的大小更小wpf,如何限制TextBox的MaxLength?
編輯:
等等,什麼?你想限制字符的數量的文本框的寬度?爲什麼?
EDIT2:
您可以通過使用Graphics.MeasureString衡量一個字符串的長度。下面是一個擴展方法,你想要做什麼:
public static class TextBoxExtension
{
public static int VisibleCharCount(this textBox textBox)
{
int count = 0;
do {
count++;
var testString = new string('X', count);
var stringWidth = System.Drawing.Graphics.MeasureString(testString, textBox.Font);
} while (stringWidth < textBox.Width);
if (stringWidth == textBox.Width)
return count;
else
return count-1;
}
}
使用方法如下:
myTextBox.MaxLength = myTextBox.VisibleCharCount();
EDIT3:
如果您TexBox是多行,你也想借此高度考慮到,你可以使用帶有Size的MeasureString的重載。我把它留給你來相應地修改我的例子。
使用TextBox
上的MaxLength屬性,該屬性在初始化後可能非常靜態。
OR
您可以用PreviewTextInput
事件取消文本輸入:
textbox.PreviewTextInput += textbox_PreviewTextInput;
void textbox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
TextBox box = (TextBox) sender;
e.Handled = box.Text.Length > 5;
}
這是形式,而不是WPF – Arcturus 2010-12-02 12:41:46
這是report.user的模板可以繪製文本框。限制是爲了避免打印問題。 – doull 2010-12-02 13:03:09