Search code examples
validationwpfdatagriddatatemplatecateldatagridviewcombobox

Validation tool tip appears twice on datatemplate combobox + Catel


Any idea why i get 2 "tool tip" messages of validation in this control in the combobox inside the datagrid on edit mode when i hover over it. In the validation summary it only appears once. On the DatagridTextColumn the validation "tool tip" appears only once in edit mode.

<DataGrid CanUserAddRows="True" CanUserDeleteRows="True" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserResizeColumns="False" ItemsSource="{Binding Detalle_OC, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" 
                      AutoGenerateColumns="False" Height="200" SelectionMode="Single" Name="Detalle_OC" EnableRowVirtualization="True">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Producto" Width="400">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Producto}"></TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>

                                        <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=catel:DataWindow}, Path=DataContext.Productos}" 
                                          Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=catel:DataWindow}, Path=DataContext.ProductoText, Mode=OneWayToSource}"
                                          SelectedValue="{Binding Id_Producto, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" SelectedValuePath="Id_Producto" DisplayMemberPath="Producto"
                                          IsTextSearchEnabled="False" StaysOpenOnEdit="True" HorizontalAlignment="Left" Width="350" IsEditable="True" behaviors:ComboBoxBehavior.CharacterCasing="Upper">
                                            <ComboBox.Triggers>
                                                <EventTrigger RoutedEvent="TextBoxBase.TextChanged">
                                                    <BeginStoryboard>
                                                        <Storyboard>
                                                            <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen">
                                                                <DiscreteBooleanKeyFrame Value="True"
                                                    KeyTime="0:0:0" />
                                                            </BooleanAnimationUsingKeyFrames>
                                                        </Storyboard>
                                                    </BeginStoryboard>
                                                </EventTrigger>
                                            </ComboBox.Triggers>
                                        </ComboBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Cantidad" Width="100" Binding="{Binding Cantidad, ValidatesOnDataErrors=True, NotifyOnValidationError=True, TargetNullValue='' }"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>

This is the model validator.

public class CDetalle_OCValidator:ValidatorBase<cDetalle_OC>
    {
        protected override void ValidateFields(cDetalle_OC instance, List<IFieldValidationResult> validationResults)
        {
            if (!instance.Id_Producto.HasValue || instance.Id_Producto<=0)
            {
                validationResults.Add(FieldValidationResult.CreateError(cDetalle_OC.Id_ProductoProperty, "El producto es requerido"));
            }

            if (!instance.Cantidad.HasValue || instance.Cantidad.Value<=0)
            {
                validationResults.Add(FieldValidationResult.CreateError(cDetalle_OC.CantidadProperty, "La cantidad tiene que ser mayor a 0"));
            }
        }

        protected override void ValidateBusinessRules(cDetalle_OC instance, List<IBusinessRuleValidationResult> validationResults)
        {
            // No business rules validations yet
        }
    }

Solution

  • The cause of the problem was the use of both ValidatesOnDataErrors and NotifyOnValidationError. Only one should be used at a time.