private void detailGrid_CellValueChanged(object sender, Control.DataGridCellEditEndingEventArgs e) { if (e.EditAction == Control.DataGridEditAction.Commit) { WPFdataGrid.DataGridControl wPFdataGrid = elementHost1.Child as WPFdataGrid.DataGridControl; var dataGridView = wPFdataGrid.grid; Control.DataGridRow dataRow = e.Row as Control.DataGridRow; PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(dataRow.Item); Int16.TryParse(properties["CapRate"]?.GetValue(dataRow.Item)?.ToString(), out Int16 CapRate); Int16.TryParse(properties["ExpDrop"]?.GetValue(dataRow.Item)?.ToString(), out Int16 ExpDrop); pokemonCapRate = new PokemonCapRate() { PName = properties["PName"].GetValue(dataRow.Item)?.ToString(), CapRate = CapRate, ExpDrop = ExpDrop }; ValidationContext validate = new ValidationContext(pokemonCapRate, null, null); IList <ValidationResult> errors = new List <ValidationResult>(); if (!Validator.TryValidateObject(pokemonCapRate, validate, errors, true)) { e.Cancel = true; EventArgs.Cancel = true; EventArgs.Error = null; Control.DataErrorValidationRule validationRule = new Control.DataErrorValidationRule(); Control.ValidationError error = new Control.ValidationError(validationRule, dataRow.BindingGroup.BindingExpressions); EventArgs.Error = $"{errors.First().ErrorMessage}"; error.ErrorContent = $"{errors.First().ErrorMessage}"; foreach (ValidationResult result in errors.Skip(1)) { EventArgs.Error += $"\n{result.ErrorMessage}"; error.ErrorContent += $"\n{result.ErrorMessage}"; } foreach (var binding in dataRow.BindingGroup.BindingExpressions) { Control.Validation.MarkInvalid(dataRow.BindingGroup.BindingExpressions.First(), error); } } else { Control.DataErrorValidationRule validationRule = new Control.DataErrorValidationRule(); Control.ValidationError error = new Control.ValidationError(validationRule, dataRow.BindingGroup.BindingExpressions); e.Cancel = false; EventArgs.Cancel = false; error.ErrorContent = null; pokemonCapRates.Add(pokemonCapRate); } } }
private void dataGrid_CellValueChanged(object sender, Control.DataGridCellEditEndingEventArgs e) { if (e.EditAction == Control.DataGridEditAction.Commit) { WPFdataGrid.DataGridControl wPFdataGrid = elementHost1.Child as WPFdataGrid.DataGridControl; var dataGridView = wPFdataGrid.grid; Control.DataGridRow dataRow = e.Row as Control.DataGridRow; PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(dataRow.Item); DataGridDetailsPresenter presenter = FindVisualChild <DataGridDetailsPresenter>(dataRow); DataTemplate detail = presenter.ContentTemplate; Int16.TryParse(properties["HP"].GetValue(dataRow.Item).ToString(), out Int16 HP); Int16.TryParse(properties["Attack"].GetValue(dataRow.Item).ToString(), out Int16 Attack); Int16.TryParse(properties["Defense"].GetValue(dataRow.Item).ToString(), out Int16 Defense); Int16.TryParse(properties["SPAttack"].GetValue(dataRow.Item).ToString(), out Int16 SPAttack); Int16.TryParse(properties["SPDefense"].GetValue(dataRow.Item).ToString(), out Int16 SPDefense); Int16.TryParse(properties["Speed"].GetValue(dataRow.Item).ToString(), out Int16 Speed); Int16 CapRate = 0; Int16 ExpDrop = 0; try { Control.DataGrid detailGrid = detail.FindName("details", presenter) as Control.DataGrid; PropertyDescriptorCollection detailProperties = TypeDescriptor.GetProperties(detailGrid.Items.CurrentItem); Int16.TryParse(detailProperties["CapRate"]?.GetValue(detailGrid.Items?.CurrentItem)?.ToString(), out CapRate); Int16.TryParse(detailProperties["ExpDrop"]?.GetValue(detailGrid.Items?.CurrentItem)?.ToString(), out ExpDrop); } catch { using (Pokemon db = new Pokemon()) { try { CapRate = db.PokemonBaseStats.Where(p => p.PName == properties["PName"].GetValue(dataRow.Item).ToString()).Single().PokemonCapRate.CapRate; ExpDrop = db.PokemonBaseStats.Where(p => p.PName == properties["PName"].GetValue(dataRow.Item).ToString()).Single().PokemonCapRate.ExpDrop; } catch { CapRate = 0; ExpDrop = 0; } } } pokemon = new PokemonBaseStat() { PName = properties["PName"].GetValue(dataRow.Item)?.ToString(), HP = HP, Attack = Attack, Defense = Defense, SPAttack = SPAttack, SPDefense = SPDefense, Speed = Speed, Type1 = properties["Type1"].GetValue(dataRow.Item)?.ToString(), Type2 = properties["Type2"].GetValue(dataRow.Item)?.ToString(), PokemonCapRate = new PokemonCapRate() { PName = properties["PName"].GetValue(dataRow.Item)?.ToString(), CapRate = CapRate, ExpDrop = ExpDrop } }; ValidationContext validate = new ValidationContext(pokemon, null, null); IList <ValidationResult> errors = new List <ValidationResult>(); if (!Validator.TryValidateObject(pokemon, validate, errors, true)) { e.Cancel = true; EventArgs.Cancel = true; EventArgs.Error = null; Control.DataErrorValidationRule validationRule = new Control.DataErrorValidationRule(); Control.ValidationError error = new Control.ValidationError(validationRule, dataRow.BindingGroup.BindingExpressions); EventArgs.Error = $"{errors.First().ErrorMessage}"; error.ErrorContent = $"{errors.First().ErrorMessage}"; foreach (ValidationResult result in errors.Skip(1)) { EventArgs.Error += $"\n{result.ErrorMessage}"; error.ErrorContent += $"\n{result.ErrorMessage}"; } foreach (var binding in dataRow.BindingGroup.BindingExpressions) { Control.Validation.MarkInvalid(dataRow.BindingGroup.BindingExpressions.First(), error); } } else { Control.DataErrorValidationRule validationRule = new Control.DataErrorValidationRule(); Control.ValidationError error = new Control.ValidationError(validationRule, dataRow.BindingGroup.BindingExpressions); e.Cancel = false; EventArgs.Cancel = false; error.ErrorContent = null; } } }