I'm new to wpf, I need to use a WPF DataGrid which has its ItemSource set to an ObservableCollection of type Model, where Model implements the IDataErrorInfo class. The problem I am facing is that if the Model returns a validation string for any property, then the user is unable to exit the edit mode for the cell, I tried rollback & I even tried CancelEdit, but I can't exit the edit mode. I searched on msdn and I found out the its one of DataGrid's property but I need to do the same because of some application requirements.
My Model Class:
public class Model: IDataErrorInfo
{
public int PropertyName{ get; set; }
// other properties & methods removed for clarity
public string this[columnName]
{
get
{
if (PropertyName< 0)
return "Error Message";
else
return string.Empty;
}
}
}
Now if 'PropertyName' is less than 0, the user cannot exit the edit mode of the specific cell.
This link on msdn says in 'Remarks' that
The DataGrid will not exit cell editing mode until the validation error is resolved.
Is there any workaround to exit the cell edit mode even if the Validation has returned an error message? I can't help with the code architecture because I am stuck with DataGrid as well as the 'Model' class. Any help would be appreciated, Thanks a lot in advance.
This is true but can overcome with a work around - using the TextBox in the DataGridTemplateColumn.CellEditingTemplate and DataGridTemplateColumn.CellTemplate.
Define a datatemplate for datagrid
assign this data template to CellTemplate and CellEditingTemplate of the data grid
In case you want to assign tool tip to show error both cases i.e. cell edit mode and mouse hover cell. You need a style
and include this style as well with the above datagrid i.e.