示例#1
0
        private void dataGrid_RowDetailsVisibilityChanged(object sender, Control.DataGridRowDetailsEventArgs e)
        {
            WPFdataGrid.DataGridControl dataGrid = elementHost1.Child as WPFdataGrid.DataGridControl;
            Control.DataGrid            grid     = dataGrid.grid;

            Control.DataGrid data = e.DetailsElement.FindName("details") as Control.DataGrid;

            Control.DataGridRow dataRow = e.Row as Control.DataGridRow;

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(dataRow.Item);

            var PName = properties["PName"]?.GetValue(dataRow.Item)?.ToString();

            using (Pokemon db = new Pokemon())
            {
                if (detailsObservable == null || !(detailsObservable.Any(p => p.PName == PName)) && db.PokemonBaseStats.Any(p => p.PName == PName))
                {
                    var monsterDetails = (from p in db.PokemonBaseStats
                                          select p.PokemonCapRate).ToList();

                    foreach (var monster in monsterDetails)
                    {
                        detailsObservable.Add(monster);
                    }

                    ListCollectionView detailsView = new ListCollectionView(detailsObservable);

                    detailsView.Filter = (p) => {
                        PokemonCapRate capRate = p as PokemonCapRate;
                        if (capRate.PName == PName)
                        {
                            return(true);
                        }
                        return(false);
                    };

                    if (!(monsterDetails == null))
                    {
                        data.ItemsSource = detailsView;
                    }
                }
                else
                {
                    data.ItemsSource = (from p in detailsObservable
                                        where p.PName == PName
                                        select p).ToList();
                }
            }

            data.IsReadOnly      = grid.IsReadOnly;
            data.SelectedIndex   = -1;
            data.CellEditEnding += new EventHandler <Control.DataGridCellEditEndingEventArgs>(detailGrid_CellValueChanged);
        }
示例#2
0
        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);
                }
            }
        }