简体   繁体   中英

Check wpf textbox focus

In my Textbox , I've set Textbox.MaxLength = 2 . What I want to achieve is when I enter "6" in my Textbox , if the focus of the Textbox is lost, the value will become "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");
        }

I've tried to set Texbox.IsFocused == false , but it still doesn't work. Any suggestions?

use LostFocus event.

xaml

<TextBox x:Name="tbTextBox" Height="30" LostFocus="tbTextBox_LostFocus"/>

code behind

private void tbTextBox_LostFocus(object sender, RoutedEventArgs e)
{
    tbTextBox.Text = tbTextBox.IsFocused.ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM