Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/guorg/gu.wpf.validationscope

Form validation for WPF
https://github.com/guorg/gu.wpf.validationscope

Last synced: about 4 hours ago
JSON representation

Form validation for WPF

Awesome Lists containing this project

README

        

# Gu.Wpf.ValidationScope

[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)
[![Gitter](https://badges.gitter.im/JohanLarsson/Gu.Wpf.ValidationScope.svg)](https://gitter.im/JohanLarsson/Gu.Wpf.ValidationScope?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![NuGet](https://img.shields.io/nuget/v/Gu.Wpf.ValidationScope.svg)](https://www.nuget.org/packages/Gu.Wpf.ValidationScope/)
[![Build status](https://ci.appveyor.com/api/projects/status/omv9baijykp70dfr?svg=true)](https://ci.appveyor.com/project/JohanLarsson/gu-wpf-validationscope)
[![Build Status](https://dev.azure.com/guorg/Gu.Wpf.ValidationScope/_apis/build/status/GuOrg.Gu.Wpf.ValidationScope?branchName=master)](https://dev.azure.com/guorg/Gu.Wpf.ValidationScope/_build/latest?definitionId=13&branchName=master)

Library that provides functionality for form validation in WPF.
It works by adding bindings to Validation.Errors for elements in a validation scope.
As bindings are somewhat expensive no bindings are added by default. The types to track errors for are specified using the `Scope.ForInputTypes`

The samples assumes an xml namespace alias `xmlns:validation="https://github.com/JohanLarsson/Gu.Wpf.ValidationScope"` is defined.

- [1. Sample:](#1-sample)
- [2. Scope](#2-scope)
- [2.1. ForInputTypes](#21-forinputtypes)
- [2.1.1. Sample: Defining a scope for textboxes.](#211-sample--defining-a-scope-for-textboxes)
- [2.1.2. Sample: Defining a partial scope for textboxes.](#212-sample--defining-a-partial-scope-for-textboxes)
- [2.1.3 Valid values](#213-valid-values)
- [2.1.3.1 Typenames](#2131-typenames)
- [2.1.3.2 Fully qualified Typenames](#2132-fully-qualified-typenames)
- [2.1.3.3 IEnumebarble](#2133-ienumebarble-t)
- [2.1.3.4 InputTypeCollection.Default](#2134-inputtypecollectiondefault)
- [2.1.3.5 InputTypes markupextension](#2135-inputtypes-markupextension)
- [2.2. HasError](#22-haserror)
- [2.3. Errors](#23-errors)
- [2.4. Node](#24-node)
- [2.5. ErrorEvent](#25-errorevent)
- [2.6. ErrorsChangedEvent](#26-errorschangedevent)
- [2.7. OneWayToSourceBindings](#27-onewaytosourcebindings)
- [3. InputTypeCollection](#3-inputtypecollection)
- [3.1. Default](#31-default)
- [4. InputTypes markupextension](#4-inputtypes-markupextension)
- [5. Node](#5-node)
- [5.1. HasError](#51-haserror)
- [5.2. Errors](#52-errors)
- [5.3. Children](#53-children)
- [5.4 Node types](#54-node-types)
- [5.4.1 InputNode](#541-inputnode)
- [5.4.2 ScopeNode](#542-scopenode)
- [5.4.3 ValidNode](#543-validnode)

# 1. Sample:
*BoolToBrushConverter is not included in the nuget. Check the demo project if interested.

```xaml





























```

Renders:

![ItemsSource2D render](http://i.imgur.com/EkuWA9c.gif)

More samples in the demo project

# 2. Scope
## 2.1. ForInputTypes
By setting `ForInputTypes` we specify what type of elements to track in the validation scope. Most of the times only TextBoxes will be relevant.
The `ForInputTypes` inherits so setting it on one element sets it to all chidlren unless explicitly set to something else for a child.
Setting `validation:Scope.ForInputTypes="{x:Null}"` means that errors are not tracked for nodes below this element.
Setting `validation:Scope.ForInputTypes="Scope"` means that only errors from subscopes are tracked.
The default value is null meaning no scope is defined.

### 2.1.1. Sample: Defining a scope for textboxes.
```xaml







```

### 2.1.2. Sample: Defining a partial scope for textboxes.
```xaml









```

### 2.1.3 Valid values
Types must be elements deriving from `UIElement`
#### 2.1.3.1 Typenames
``

#### 2.1.3.2 Fully qualified Typenames
``

#### 2.1.3.3 IEnumebarble
``

#### 2.1.3.4 InputTypeCollection.Default
``

#### 2.1.3.5 InputTypes markupextension
``

## 2.2. HasError
A bool indicating if there is a validation error in the scope. Similar to `System.Controls.Validation.HasError`

```xaml




```

## 2.3. Errors
A `ReadOnlyObservableCollection` with the errors in the scope. Similar to `System.Controls.Validation.Errors`

```xaml





```

## 2.4. Node
A `Node` in the tree that is the validation scope.

```xaml





```

## 2.5. ErrorEvent
An event that notifies when errors are added and removed. Similar to `System.Windows.Controls.Validation.ErrorEvent`
Does not require bindings to have `NotifyOnValidationError=True`

```xaml




```

## 2.6. ErrorsChangedEvent
An event that notifies when errors are added and removed. The `ErrorEvent` notifies about each add and remove while `ErrorsChangedEvent`notifies once with a all added and removed events.
Does not require bindings to have `NotifyOnValidationError=True`

```xaml




```

## 2.7. OneWayToSourceBindings
WPF does not allow binding readonly dependency properties even with `Mode=OneWayToSource`.
As a workaround for this OneWayToSourceBindings can be used like this:

```xaml





```

# 3. InputTypeCollection
## 3.1. Default
Contains the following types `{ typeof(Scope), typeof(TextBoxBase), typeof(Selector), typeof(ToggleButton), typeof(Slider) }`
And should be enough for most scenarios when you don't have third party controls for example a third party textbox that does not derive from `TextBoxBase`

# 4. InputTypes markupextension
Exposed for convenience to create list of types in xaml.

``

# 5. Node
The validation scope is a tree of nodes.
## 5.1. HasError
A bool indicating if there is a validation error in the scope. Similar to `System.Controls.Validation.HasError`

## 5.2. Errors
A `ReadOnlyObservableCollection` with the errors in the scope. Similar to `System.Controls.Validation.Errors`

## 5.3. Children
A `ReadOnlyObservableCollection` with the child nodes which have errors in the scope.

## 5.4 Node types

### 5.4.1 InputNode
This node type is used for elements for which we track errors.

### 5.4.2 ScopeNode
This node type is used for elements which has subnodes with errors.
This node type does not listen to validation errors for its source element.

### 5.4.3 ValidNode
This node type is used for elements which has no errors or are not in a scope.
This is immutable and a single instance is used for all.