My class, which implements IDataErrorInfo, returns error message such way:
<pre lang="cs">public string Error
{
get
{
StringBuilder sb = new StringBuilder();
if (NameOfWear.Length > 5 && Convert.ToInt16(Price) < 20000)
return sb.Append("Price is too small").ToString();
return sb.ToString();
}
}
This error is updated only at the start of application, or when I sort items in dataGrid, or when i call GridComponent.Reset(something like that). But I change this elemet from another textbox, both elements are binded . When I change textbox, element of dataGrid automatically changes too, and simle exceptions work onPropertyChanges and CollectionChanes,such as:
<pre lang="cs">public string this[string columnName]
{
get
{
string result = "";
int i;
if (columnName == "NameOfWear")
{
if (string.IsNullOrWhiteSpace(NameOfWear))
{
result = "Enter any name";
}
else if (NameOfWear.Length < 3)
{
result = "Correct name plz";
}
else if(int.TryParse(NameOfWear,out i))
{
result="No numbers";
}
}
else if (columnName == "Price")
{
if (!int.TryParse(Price, out i))
{
result = "Only numbers";
}
}
return result;
How to make this exception to be displayed on PropertyChanged, not when GridReset,etc?
P.S. MY XAML:
<<pre lang="xml">Page x:Class="WpfBrowserApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfBrowserApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="307"
Title="Page1">
<Page.Resources>
<Style x:Key="ErrorStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Padding" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel >
<TextBox Margin="3" IsReadOnly="True">Название</TextBox>
<TextBox Style="{StaticResource ResourceKey=ErrorStyle}" Margin="3" Text="{Binding Path=wear.NameOfWear, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" ></TextBox>
<TextBox Margin="3" IsReadOnly="True" >Цена</TextBox >
<TextBox Style="{StaticResource ResourceKey=ErrorStyle}" Margin="3" Text="{Binding Path=wear.Price, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"></TextBox>
<StackPanel Orientation="Horizontal">
<Button HorizontalAlignment="Stretch" Margin="3" Command="{Binding Path=AddRowCommand}" >Добавить поле</Button>
<Button HorizontalAlignment="Stretch" Margin="3" Command="{Binding Path=RemoveRowCommand}" Content="RemoveRow"></Button>
</StackPanel>
</StackPanel>
<DataGrid RowStyle="{StaticResource RowStyle}" ItemsSource="{Binding Path=Collection, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" SelectedItem="{Binding Path=wear, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch" IsReadOnly="False" Grid.Row="1">
<DataGrid.RowValidationRules>
<DataErrorValidationRule ValidationStep="UpdatedValue" ValidatesOnTargetUpdated="True">
</DataErrorValidationRule>
</DataGrid.RowValidationRules>
<DataGrid.Columns >
<DataGridTextColumn Header="Name" Binding="{Binding Path=NameOfWear, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Width="*"></DataGridTextColumn>
<DataGridTextColumn Header="Price" Binding="{Binding Path=Price, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Page>
P.S.2 Everything works fine,except this.
P.S.3 Sorry for bad English /