Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在WPF中使用事件_Wpf_Vb.net_Validation_Events - Fatal编程技术网

在WPF中使用事件

在WPF中使用事件,wpf,vb.net,validation,events,Wpf,Vb.net,Validation,Events,我有两个WPF文本框。命名为txt1和txt2 在txt1的lostFocus中,我写道 If txt1.Text is nothing then txt1.Focus End If 在txt2的lostFocus事件中,我写入 If txt2.Text is nothing then txt2.Focus End If 现在,如果txt1和txt2都为空,并且用户在txt1中按下TAB键,那么问题就会出现。程序在无限循环中运行。我的意思是光标无限次地指向txt1和txt2

我有两个WPF文本框。命名为txt1和txt2

在txt1的lostFocus中,我写道

If txt1.Text is nothing then 
    txt1.Focus
End If
在txt2的lostFocus事件中,我写入

If txt2.Text is nothing then
    txt2.Focus
End If
现在,如果txt1和txt2都为空,并且用户在txt1中按下TAB键,那么问题就会出现。程序在无限循环中运行。我的意思是光标无限次地指向txt1和txt2。根据我的代码,我知道这是正常的行为

因此,我希望使用验证事件来避免上述问题。但我在WPF里找不到。
那么我应该使用哪个事件呢?

我不是VB程序员,所以不能为您编写准确的代码,但下面是您应该做的。为事件PreviewLostKeyboardFocus添加事件处理程序。在事件处理程序内部,如果文本为空,则将e.Handled设置为true。 示例C#代码。我已经编写了一个通用处理程序

private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    if (string.IsNullOrEmpty((sender as TextBox).Text))
    {
        e.Handled = true;
    }
}

更好的解决方案可能是允许用户导航离开空/空文本框,但将文本还原为初始值(如果有)或提供验证错误。使用IDataErrorInfo提供验证错误相对容易

作为一名软件用户,当应用程序阻止我离开某个领域时,我会感到恼火

重置值方法

有关如何维护和获取上一个值的信息,请参阅此stackoverflow appraoch。在LostFocus事件中,如果当前值无效,可以将成员变量设置回_oldValue。

验证方法

这两个日期存储在模型或视图模型类中。在该类中实现IDataErrorInfo()。然后在xaml中可以显示验证错误

//This is your model/viewmodel validation logic
public string this[string columnName]
{
    get
    {
        string result = null;
        if (columnName == "FirstName")
        {
            if (string.IsNullOrEmpty(FirstName))
                result = "Please enter a First Name";
        }
        if (columnName == "LastName")
        {
            if (string.IsNullOrEmpty(LastName))
                result = "Please enter a Last Name";
        }
       if (columnName == "Age")
        {
            if (Age < = 0 || Age >= 99)
                result = "Please enter a valid age";
        }
        return result;
    }
}

//Here is a sample of a xaml text block
<textbox x:Name="tbFirstName" Grid.Row="0" Grid.Column="1" Validation.Error="Validation_Error" Text="{Binding UpdateSourceTrigger=LostFocus, Path=FirstName, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />
//这是您的模型/视图模型验证逻辑
公共字符串此[string columnName]
{
得到
{
字符串结果=null;
如果(columnName==“FirstName”)
{
if(string.IsNullOrEmpty(FirstName))
结果=“请输入一个名字”;
}
如果(columnName==“LastName”)
{
if(string.IsNullOrEmpty(LastName))
结果=“请输入姓氏”;
}
如果(columnName==“年龄”)
{
如果(年龄<=0 | |年龄>=99)
结果=“请输入有效年龄”;
}
返回结果;
}
}
//下面是一个xaml文本块的示例
你也可以看看其他的StackOverflow帖子-

与其将焦点设置回失去焦点的文本框,为什么不用红色勾勒文本框,显示字段需要数据或显示消息框?或者,为什么不在到达表单末尾并且用户按下提交按钮之前保持验证?我的会计项目已经开始了。我需要用户提供两个日期。财政年度开始日期和财政年度结束日期都是必需的。那么,在这种情况下,您有什么建议?在这两个字段通过验证(不为空)之前,不要启用下一个操作。谢谢您的建议。但是这个问题已经被Nishant解决了。那由于鼠标而失去焦点呢?@Vishal这不是你之前问的问题。你有没有试过我建议的代码?即使你使用鼠标,这段代码也会把焦点放回文本框上。我由衷地抱歉忽略了你的答案。这是一个完美的答案。