单击按钮时,VB.net使用IDataErrorInfo验证文本框

时间:2019-02-28 10:08:12

标签: wpf vb.net

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

<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

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

0 个答案:

没有答案