ComponentOne InputPanel for WPF
Features / Data Validation / Data Validation through Event
In This Topic
    Data Validation through Event
    In This Topic

    InputPanel provides another way of handling input validation through event. The control comes with the ValidateCurrentItem event that can be used for validating user input.

    The following image shows validation applied through event.

    To implement data validation through event, subscribe the ValidateCurrentItem event in code and add validation rules in the event handler. The following code shows how to apply validation using event. This example uses the sample created in the Quick start.

    Private Sub InPanel_ValidateCurrentItem(sender As Object, e As CancelEventArgs) _
        Handles InPanel.ValidateCurrentItem
        Dim inputPanel As C1InputPanel = TryCast(sender, C1InputPanel)
        Dim customer As Customer = TryCast(inputPanel.CurrentItem, Customer)
    
        If customer IsNot Nothing Then
            Dim errorList = New ObservableCollection(Of ErrorInfo)()
    
            If customer.Name IsNot Nothing AndAlso _
                String.IsNullOrWhiteSpace(customer.Name.ToString()) Then
                errorList.Add(New ErrorInfo() With { _
                    .ErrorInputName = "Name", _
                    .ErrorContent = "This field cannot be empty" _
                })
            End If
            If customer.Weight > 110 Then
                errorList.Add(New ErrorInfo() With { _
                    .ErrorInputName = "Weight", _
                    .ErrorContent = "Value out of range." _
                })
            End If
            inputPanel.ValidationErrors = errorList
            If errorList.Count > 0 Then
                e.Cancel = True
            End If
        End If
    End Sub
    
    private void InPanel_ValidateCurrentItem
        (object sender, System.ComponentModel.CancelEventArgs e)
    {
        C1InputPanel inputPanel = sender as C1InputPanel;
        Customer customer = inputPanel.CurrentItem as Customer;
    
        if (customer != null)
        {
            var errorList = new ObservableCollection<ErrorInfo>();
    
            if (customer.Name != null && 
                string.IsNullOrWhiteSpace(customer.Name.ToString()))
            {
                errorList.Add(new ErrorInfo { ErrorInputName = "Name", 
                    ErrorContent = "This field cannot be empty." });
            }
            if (customer.Weight > 110)
            {
                errorList.Add(new ErrorInfo { ErrorInputName = "Weight", 
                    ErrorContent = "Value out of range." });
            }
            inputPanel.ValidationErrors = errorList;
            if (errorList.Count > 0)
            {
                e.Cancel = true;
            }
        }
    }
    
    See Also