title: VB.net Validate Textbox using IDataErrorInfo When click on Button
body: <p>I want to create a simple Login system using WPF MaterialDesign control. I want to validate Username and Password When Click on Login button and check if that fields are empty or not. here is my view:</p>
  
  <pre><code>&lt;Grid&gt;
          &lt;StackPanel Background="White" Grid.Column="1"&gt;
              &lt;TextBlock Text="SignIn" FontSize="30" TextAlignment="Center" Margin="0,60,0,30"/&gt;
              &lt;StackPanel Orientation="Horizontal" Margin="32,0,32,0"&gt;
                  &lt;materialDesign:PackIcon Kind="Person" Width="20" VerticalAlignment="Center" Margin="0,0,10,0"/&gt;
                  &lt;TextBox 
                      x:Name="username" 
                      Text="{Binding Username, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                      materialDesign:HintAssist.Hint="Username" 
                      Width="215"/&gt;
              &lt;/StackPanel&gt;
              &lt;StackPanel Orientation="Horizontal" Margin="32,20,32,0"&gt;
                  &lt;materialDesign:PackIcon Kind="Key" Width="20" VerticalAlignment="Center" Margin="0,0,10,0"/&gt;
                  &lt;TextBox Text="{Binding Password}" materialDesign:HintAssist.Hint="Password"  Width="215"/&gt;
              &lt;/StackPanel&gt;
  
              &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,40,0,0"&gt;
                  &lt;Button Content="LOGIN" Width="100" Margin="0,0,20,0"  Command="{Binding LoginCmd}"/&gt;
              &lt;/StackPanel&gt;
  
  
              &lt;TextBlock Text="Create New Account" TextAlignment="Center" Margin="0,10,0,0" Cursor="Hand"/&gt;
  
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  </code></pre>
  
  <p>Here is my viewmodel</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But the problem is when I run that code it shows "Username can't be blank!". But I want to show the error message only when user clicks on the "Login" button. So how to solve this?</p>