1
votes

I have a DataGrid with a Column that is binded with an 'int' type property. I am using IDataErrorInfo for validation. My validation rules are working fine they get fired as soon as I change value of a cell. Let's say user enter value less than 0, I show respective error in the tooltip. Problem comes when user clear the value of text box. In that case IDataErrorInfo never fires and as a result I cannot show user validation error through tooltip saying that value cannot be empty. DataGrid makes the red border around textbox, which is fine, and a '!' sign at row header, but no validation error in the tooltip as IDataErrorInfo never got fired. Is there anything I can do to fire IDataError info in case value of textbox is cleared?

Edit:
XAML

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayOrder, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding DisplayOrder, UpdateSourceTrigger=PropertyChanged}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

DisplayOrder Property

public int DisplayOrder
{
    get
    {
        return m_DisplayOrder;
    }
    set
    {
        if(value != m_DisplayOrder)
        {
            m_DisplayOrder = value;
            OnPropertyChanged("DisplayOrder");
        }
    }
}
2

2 Answers

2
votes

you should post your bindings and show us your itemssource object. i assume that your property you bind to is type of int, so if you clear the textbox, wpf binding try to set the int property to null. there a binding error happens cause your int is not nullable. and because of the binding error your idataerror has no chance to fire cause it would not be invoked. you can try to add ValidatesOnExceptions=true to your datagrid column binding to see an error.

0
votes

As you can see from error, StringToNumber converting is throwing, can u try to use your own IValueConverter to convert String to Int and other way around. and maybe by default it will convert empty string to 0 (where your IDataErrorInfo should kick in and show error)