the anwser here https://social.msdn.microsoft.com/Forums/vstudio/en-US/18ea596e-ae96-481c-a928-a51c23fbf912/datagrid-blankcell-validationrules-and-mvvm-problem?forum=wpf#26dadcb6-9d30-4f70-b556-89fdda8d960d helped me to apply validation rules to a
row in datagrid
i had this class to get me changed items in the datagrid
i want to stop adding the row to the changed items if it has a validation error but the "e.row.item" for "DataGridRowEditEndingEventArgs" only gets the data when i first start editing for example if i added a new row the e.row.item column "Name" will be empty even if i added data to it until i hit enter and start editing again is there is a way to get a around it? or a better way to know if a row has a validation error ?
i had this class to get me changed items in the datagrid
public class DataGridSelectedItemsBlendBehavior : Behavior<DataGrid> { public static readonly DependencyProperty HasValidationErrorProperty = DependencyProperty.Register("HasValidationError", typeof(bool), typeof(DataGridSelectedItemsBlendBehavior), new FrameworkPropertyMetadata(null) { BindsTwoWayByDefault = true }); public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(DataGridSelectedItemsBlendBehavior), new FrameworkPropertyMetadata(null) { BindsTwoWayByDefault = true }); public static readonly DependencyProperty ChangedItemProperty = DependencyProperty.Register("ChangedItems", typeof(IList), typeof(DataGridSelectedItemsBlendBehavior), new FrameworkPropertyMetadata(null) { BindsTwoWayByDefault = true }); public bool HasValidationError { get { return (bool)GetValue(HasValidationErrorProperty); } set { SetValue(HasValidationErrorProperty, value); } } public IList SelectedItems { get { return (IList)GetValue(SelectedItemProperty); } set { SetValue(SelectedItemProperty, value); } } public IList ChangedItems { get { return (IList)GetValue(ChangedItemProperty); } set { SetValue(ChangedItemProperty, value); } } protected override void OnAttached() { base.OnAttached(); this.AssociatedObject.SelectionChanged += OnSelectionChanged; this.AssociatedObject.RowEditEnding += OnRowEditEnding; this.AssociatedObject.Loaded += AssociatedObject_Loaded; } void AssociatedObject_Loaded(object sender, RoutedEventArgs e) { if (this.SelectedItems != null) { var selectedItems = this.SelectedItems; foreach (var obj in selectedItems) { var rowContainer = this.AssociatedObject.ItemContainerGenerator.ContainerFromItem(obj) as DataGridRow; if (rowContainer != null) { rowContainer.IsSelected = true; } } } } protected override void OnDetaching() { base.OnDetaching(); if (this.AssociatedObject != null) { this.AssociatedObject.SelectionChanged -= OnSelectionChanged; this.AssociatedObject.RowEditEnding -= OnRowEditEnding; this.AssociatedObject.Loaded -= AssociatedObject_Loaded; } } /// <summary> /// Add/remove row to selected items collection based on user interaction with the datagrid with shift or ctrl buttons /// </summary> private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems != null && e.AddedItems.Count > 0 && this.SelectedItems != null) { foreach (object obj in e.AddedItems) { if (obj.ToString() == "{NewItemPlaceholder}") { continue; } this.SelectedItems.Add(obj); } } if (e.RemovedItems != null && e.RemovedItems.Count > 0 && this.SelectedItems != null) { foreach (object obj in e.RemovedItems) { this.SelectedItems.Remove(obj); } } } public static bool IsValid(DependencyObject obj) { return obj == null ? false : !Validation.GetHasError(obj) && LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>().All(IsValid); } /// <summary> /// add the commited items in the datagrid to changeditems collection /// </summary> private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { if (e.EditAction == DataGridEditAction.Commit) { if (!IsValid(e.Row as DependencyObject)) { HasValidationError = true; return; } var item = ChangedItems.Cast<object>().SingleOrDefault(i => i != null); if (item == null) { this.ChangedItems.Add(e.Row.Item); } else { item = e.Row.Item; } HasValidationError = false; } } }
i want to stop adding the row to the changed items if it has a validation error but the "e.row.item" for "DataGridRowEditEndingEventArgs" only gets the data when i first start editing for example if i added a new row the e.row.item column "Name" will be empty even if i added data to it until i hit enter and start editing again is there is a way to get a around it? or a better way to know if a row has a validation error ?