繁体   English   中英

检查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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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