I have a DataGrid using an inline style that adds a tooltip for reporting errors - I bind it to a collection that implements IDataErrorInfo.
In particular, I have a column associated with an integer with IDataErrorInfo logic to prevent values ββoutside a certain range - when I break this rule, the default behavior (for example, the text box is highlighted in red) is applied instead of activating my error style, however, if I cause an error by typing text in a text box and calling the InvalidInputString format, it will cause my error style, as well as the way I want.
Here is my XAML:
<DataGrid ItemsSource="{x:Static local:WeatherForecast.TomorrowsForecast}" AutoGenerateColumns="False"> <DataGrid.Resources> <Style x:Key="errorStyle" TargetType="{x:Type TextBox}"> <Setter Property="Padding" Value="-2"/> <Style.Triggers> <Trigger Property="Validation.HasError" Value="True"> <Setter Property="Background" Value="PeachPuff"/> <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/> </Trigger> </Style.Triggers> </Style> </DataGrid.Resources> <DataGrid.Columns> <DataGridTextColumn Header="City" Binding="{Binding Path=Planet}"/> <DataGridTextColumn Header="Low Temperature" Binding="{Binding Path=LowestTemp, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}" EditingElementStyle="{StaticResource errorStyle}" /> <DataGridTextColumn Header="High Temperature" Binding="{Binding Path=HighestTemp, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}" EditingElementStyle="{StaticResource errorStyle}" /> </DataGrid.Columns> </DataGrid>
My simple logic is IDateErrorInfo:
public string this[string columnName] { get { // Temperature range checks. if ((columnName == "LowestTemp") || (columnName == "HighestTemp")) { if ((this.LowestTemp < -273) || (this.HighestTemp < -273)) { return "Temperature can not be below the absolute cold (-273Β°C)."; } } // All validations passed successfully. return null; } }
Why is the default error checking behavior for the red border but not my style?
UPDATE:
This seems to be fine when done OUTSIDE DATA DataGrid, for example, if I have two homeless text fields attached to the same instance of my object
<TextBlock>Lowest Temp</TextBlock> <TextBox Width="100" DataContext="{StaticResource instance}" Text="{Binding Path=LowestTemp, NotifyOnValidationError=True, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}" Style="{StaticResource errorStyle}" /> <TextBlock>Highest Temp</TextBlock> <TextBox Width="100" DataContext="{StaticResource instance}" Text="{Binding Path=HighestTemp, NotifyOnValidationError=True, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}" Style="{StaticResource errorStyle}" />
All perfectly! Any idea that this applies to internal DataGrid objects that might interfere with this behavior?