I want to notify the validation errors when i give wrong input in the Rows of the DataGrid.
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"/>
<DataGridTextColumn Header="Age" Binding="{Binding Path=Age}"/>
<DataGridTextColumn Header="Date Of Birth" Binding="{Binding Path=DateOfBirth, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/>
i implemented the IDataErrorInfo in the ViewModel with the following property.
public string this[string propname]
{
get
{
switch (propname)
{
case "Age":
int age=0;
if (int.TryParse("Age", out age))
{
if (age <= 0 && age > 99)
{
return "Please enter the valid Age...";
}
}
else
return "Please enter the valid Age...";
break;
case "Name":
if (string.IsNullOrEmpty("Name"))
{
return "Enter the valid Name";
}
break;
case "Address":
if (string.IsNullOrEmpty("Address"))
{
return "Enter the valid Address";
}
break;
case "DateOfBirth":
DateTime datetime;
if (!DateTime.TryParse("DateOfBirth", out datetime))
{
return "Please enter the valid Date of Birth";
}
break;
}
return string.Empty;
}
}
But the validation is not happened. I need the requirement that , if i type the text to the DateTime property in the DataGridCell, there should be red balloon to indicate the validation error..
Is it possible ?.. Anyone ?
This line:
if (int.TryParse("Age", out age))
cannot be correct.
If you want a red balloon to show up you need to provide the red balloon:
<Style x:Key="TextBlockStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
And:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource ResourceKey=TextBlockStyle}"
Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
I leave it to you to make it red.