检查 wpf 文本框焦点
Check wpf textbox focus
在我的 Textbox
中,我设置了 Textbox.MaxLength = 2
。我想要实现的是当我在 Textbox
中输入“6”时,如果 Textbox
的焦点丢失,该值将变为“06”。
if (!(string.IsNullOrEmpty(Textbox.Text)))
{
//Minute value cannot be more than 59
if (Int32.Parse(Textbox.Text) > 59)
Textbox.Text = string.Empty;
else if (Int32.Parse(Textbox.Text) < 10 && Textbox.IsFocused == false)
Textbox.Text = Int32.Parse(Text.Text).ToString("00");
}
我试过设置Texbox.IsFocused == false
,但还是不行。有什么建议吗?
使用 LostFocus 事件。
xaml
<TextBox x:Name="tbTextBox" Height="30" LostFocus="tbTextBox_LostFocus"/>
代码隐藏
private void tbTextBox_LostFocus(object sender, RoutedEventArgs e)
{
tbTextBox.Text = tbTextBox.IsFocused.ToString();
}
在我的 Textbox
中,我设置了 Textbox.MaxLength = 2
。我想要实现的是当我在 Textbox
中输入“6”时,如果 Textbox
的焦点丢失,该值将变为“06”。
if (!(string.IsNullOrEmpty(Textbox.Text)))
{
//Minute value cannot be more than 59
if (Int32.Parse(Textbox.Text) > 59)
Textbox.Text = string.Empty;
else if (Int32.Parse(Textbox.Text) < 10 && Textbox.IsFocused == false)
Textbox.Text = Int32.Parse(Text.Text).ToString("00");
}
我试过设置Texbox.IsFocused == false
,但还是不行。有什么建议吗?
使用 LostFocus 事件。
xaml
<TextBox x:Name="tbTextBox" Height="30" LostFocus="tbTextBox_LostFocus"/>
代码隐藏
private void tbTextBox_LostFocus(object sender, RoutedEventArgs e)
{
tbTextBox.Text = tbTextBox.IsFocused.ToString();
}