首页 > 解决方案 > VB.net 在单击按钮时使用 IDataErrorInfo 验证文本框

问题描述

我想使用 WPF MaterialDesign 控件创建一个简单的登录系统。我想在单击登录按钮时验证用户名和密码并检查该字段是否为空。这是我的看法:

<Grid>
        <StackPanel Background="White" Grid.Column="1">
            <TextBlock Text="SignIn" FontSize="30" TextAlignment="Center" Margin="0,60,0,30"/>
            <StackPanel Orientation="Horizontal" Margin="32,0,32,0">
                <materialDesign:PackIcon Kind="Person" Width="20" VerticalAlignment="Center" Margin="0,0,10,0"/>
                <TextBox 
                    x:Name="username" 
                    Text="{Binding Username, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                    materialDesign:HintAssist.Hint="Username" 
                    Width="215"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="32,20,32,0">
                <materialDesign:PackIcon Kind="Key" Width="20" VerticalAlignment="Center" Margin="0,0,10,0"/>
                <TextBox Text="{Binding Password}" materialDesign:HintAssist.Hint="Password"  Width="215"/>
            </StackPanel>

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,40,0,0">
                <Button Content="LOGIN" Width="100" Margin="0,0,20,0"  Command="{Binding LoginCmd}"/>
            </StackPanel>


            <TextBlock Text="Create New Account" TextAlignment="Center" Margin="0,10,0,0" Cursor="Hand"/>

        </StackPanel>
    </Grid>

这是我的视图模型

Imports System.ComponentModel

Public Class LoginViewModel
    Inherits BaseViewModel
    Implements IDataErrorInfo

    Dim LoginUser As Login

    'Get username and password
    Private _username As String
    Private _password As String

    Public Sub New()
        LoginUser = New Login
    End Sub

    Public Property Username As String
        Get
            Return _username
        End Get
        Set(value As String)
            _username = value
            RaisedPropertyChanged("Username")
        End Set
    End Property

    Public Property Password As String
        Get
            Return _password
        End Get
        Set(value As String)
            _password = value
            RaisedPropertyChanged("Password")
        End Set
    End Property



    Dim _cmd As New CustomCommand(AddressOf CheckLogin)

    Public ReadOnly Property LoginCmd As ICommand
        Get
            Return _cmd
        End Get
    End Property

    Private Sub CheckLogin()
        LoginUser.Username = _username
        LoginUser.Password = _password

        If LoginUser.CheckLogin = True Then
            MsgBox("Successfull")
        Else
            MsgBox("Not Successfull")
        End If
    End Sub


    Default Public ReadOnly Property Item(columnName As String) As String Implements IDataErrorInfo.Item
        Get
            Dim errorMsg As String = String.Empty
            If "Username" = columnName Then
                If String.IsNullOrEmpty(Me.Username) Then
                    errorMsg = "Username can't be blank!"
                End If
            End If

            Return errorMsg
        End Get
    End Property

    Public ReadOnly Property [Error] As String Implements IDataErrorInfo.Error
        Get
            Return String.Empty
        End Get
    End Property
End Class

但问题是当我运行该代码时,它显示“用户名不能为空!”。但我只想在用户单击“登录”按钮时显示错误消息。那么如何解决这个问题呢?

标签: wpfvb.net

解决方案


推荐阅读