title: How to validate Null or Empty cell in DataGrid
body: <p>I am trying to Validate a cell in DataGrid. This is my first approach to validation. I get some problems with validations as I am new to it.</p>
  
  <p>I have created a class called StringIsNullOrEmptyValidationRule.cs. This class checks if string is null or ""</p>
  
  <p>Here is the code of StringIsNullOrEmptyValidationRule.cs :</p>
  
  <pre><code>class StringIsNullOrEmptyValidationRule : ValidationRule
  {
      private string _errorMessage = "";
      public string ErrorMessage
      {
          get
          {
              return _errorMessage;
          }
          set
          {
              _errorMessage = value;
          }
      }
  
      public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
      {
          ValidationResult result = new ValidationResult(true, null);
  
          if (value == null || ((string)value).Trim() == "")
          {
              result = new ValidationResult(false, this.ErrorMessage);
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>Now I have a datagrid in MainWindow.xaml which is bound to an ObservableCollection called People. Here is my DataGrid :</p>
  
  <pre><code>&lt;DataGrid x:Name="maindg" ItemsSource="{Binding People}" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="CellOrRowHeader"&gt;
      &lt;DataGrid.Columns&gt;
  
          &lt;DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" /&gt;
  
          &lt;DataGridTextColumn Header="Last Name"&gt;
              &lt;DataGridTextColumn.Binding&gt;
                  &lt;Binding Path="LastName"&gt;
                      &lt;Binding.ValidationRules&gt;
                          &lt;local:StringIsNullOrEmptyValidationRule ErrorMessage="LastName is required" /&gt;
                      &lt;/Binding.ValidationRules&gt;
                  &lt;/Binding&gt;
              &lt;/DataGridTextColumn.Binding&gt;
          &lt;/DataGridTextColumn&gt;
  
          &lt;DataGridTextColumn Header="City" Binding="{Binding City}" /&gt;
  
      &lt;/DataGrid.Columns&gt;
  
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p><strong>Problem :</strong></p>
  
  <p>I kept a breakpoint on StringIsNullOrEmptyValidationRule's Validate Method's first line.
  When I don't enter any data in a cell under LastName Column, and try to move away from the cell, it does not hit breakpoint, that means validation does not even check.</p>
  
  <p>If I enter some data to the cell under lastName column and then try to move away from the cell, it tries to validates the cell. So it hits the breakpoint.</p>
  
  <p>So, my question is how can I validate NullOrEmpty Cell?</p>