Exemplo n.º 1
0
        private void Startup(PrefabManager prefabManager)
        {
            ItemNameField.Text = _prefabName;

            FillPrefabDataTable();
            List <DataRow> origRows = new List <DataRow>();

            foreach (DataRow row in _prefabTable.Rows)
            {
                origRows.Add(row);
            }

            _prefabTable.RowChanged += (sender, args) =>
            {
                SaveButton.IsEnabled = !string.IsNullOrEmpty(ItemNameField.Text);
                if (args.Action == DataRowAction.Add)
                {
                    args.Row[1] = "TextField";
                    _prefabTable.AcceptChanges();
                }
                else
                {
                    switch (args.Action)
                    {
                    case DataRowAction.Change when !origRows.Contains(args.Row):
                        return;

                    case DataRowAction.Change:
                        int      index = origRows.IndexOf(args.Row);
                        string[] keys  = _changedColumnNames.Keys.ToArray();

                        _changedColumnNames[keys[index]] = args.Row[0].ToString();
                        _prefabTable.AcceptChanges();
                        break;
                    }
                }

                RowPositionTextBox.MaxValue = _prefabTable.Rows.Count;
            };
            _prefabTable.RowDeleting += (sender, args) =>
            {
                if (origRows.Contains(args.Row))
                {
                    _deletedColumns.Add(args.Row[0].ToString());
                }
            };
            _prefabTable.RowDeleted += (sender, args) =>
            {
                SaveButton.IsEnabled = !string.IsNullOrEmpty(ItemNameField.Text);
                _prefabTable.AcceptChanges();
                RowPositionTextBox.MaxValue = _prefabTable.Rows.Count;
            };
            RowPositionTextBox.MaxValue = _prefabTable.Rows.Count;

            PrefabGrid.ItemsSource = _prefabTable;
            var j = 0;

            PrefabGrid.Loaded += (sender, args) =>
            {
                var comboStrings = new HashSet <string>
                {
                    "TextField",
                    "AutoComplete",
                    "CheckBox",
                    "ComboBox",
                    "DatePicker",
                    "Numeric",
                    "Currency",
                    "Hyperlink",
                    "Percentage"
                };
                if (j == 0)
                {
                    for (var i = 0; i < _prefabTable.Columns.Count; i++)
                    {
                        GridColumn column;
                        if (i == 1)
                        {
                            column = new GridComboBoxColumn
                            {
                                MappingName     = _prefabTable.Columns[i].ColumnName,
                                ItemsSource     = comboStrings,
                                StaysOpenOnEdit = true,
                                IsEditable      = false,
                                HeaderText      = _prefabTable.Columns[i].ColumnName
                            };

                            PrefabGrid.Columns.Add(column);
                        }
                        else if (i > 1)
                        {
                            column = new GridNumericColumn
                            {
                                MappingName          = _prefabTable.Columns[i].ColumnName,
                                HeaderText           = _prefabTable.Columns[i].ColumnName,
                                NumberDecimalDigits  = 0,
                                NumberGroupSeparator = ","
                            };

                            PrefabGrid.Columns.Add(column);
                        }
                        else
                        {
                            column = new GridTextColumn
                            {
                                MappingName = _prefabTable.Columns[i].ColumnName,
                                HeaderText  = _prefabTable.Columns[i].ColumnName
                            };

                            PrefabGrid.Columns.Add(column);
                        }
                    }
                }

                j++;
            };
            _prefabManager = prefabManager;

            _changedColumnNames = new Dictionary <string, string>();
            foreach (DataRow dr in _prefabTable.Rows)
            {
                _changedColumnNames.Add(dr[0].ToString(), dr[0].ToString());
            }
            _deletedColumns = new List <string>();
        }