Validation can also happen while binding is made. This is very useful if the bound objects have built in logic. For example if you have a Product object which has Price property which must be some number bigger the zero you will probably have code similar to this one:
class Product
{
private decimal _price;
public decimal Price
{
get { return _price; }
set
{
if (value <= 0)
throw new Exception("Exception from Price setter");
_price = value;
}
}
If you need to mark invalid input inside some TextBox you can then write something like this:
<Window.Resources>
<code:Product x:Key="dsProduct"/>
</Window.Resources>
<StackPanel>
<WrapPanel>
<TextBox Width="200" Text="{Binding
Source={StaticResource dsProduct},
Path=Price,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True,
ValidatesOnExceptions=True}">
</TextBox>
</WrapPanel>
</StackPanel>