Exemple #1
0
        /// <summary>
        /// Le asigna el maximo de escritura a los controles igual al que tienen en la BD
        /// </summary>
        /// <param name="obj">Objeto a validar las propiedades</param>
        /// <param name="ui">Contenedor donde va a buscar controles</param>
        /// <param name="enumMode">Modo en el que se abrió la ventana</param>
        /// <param name="blnCharacters">true. bloquea caracters especiales</param>
        /// <history>
        /// [emoguel] created 08/04/2016
        /// [emoguel] modified 11/07/2016
        /// [erosado] Modified. 12/08/2016. Se agrego para que acepte el MaxLenght de las cajas de texto o si no tuviera aceptaria las de la propiedad MaxLengthPropertyClass
        /// </history>
        public static void SetUpControls <T>(T obj, UIElement ui, EnumMode enumMode = EnumMode.ReadOnly, bool blnCharacters = false, EnumDatabase database = EnumDatabase.IntelligentMarketing) where T : class
        {
            List <Control> lstControls = GetChildParentCollection <Control>(ui);//Obtenemos la lista de controles del contenedor
            List <Model.Classes.ColumnDefinition> lstColumnsDefinitions = BRHelpers.GetFieldsByTable <T>(obj, database);

            Type type = obj.GetType();//Obtenemos el tipo de objeto

            if (lstControls.Count > 0)
            {
                #region DataGrid
                List <DataGrid> lstDataGrids = lstControls.Where(cl => cl is DataGrid && !((DataGrid)cl).IsReadOnly).OfType <DataGrid>().ToList();
                lstDataGrids.ForEach(dtg => dtg.Sorting += GridHelper.dtg_Sorting);
                #endregion

                #region Obtenemos el MaxLength
                foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(pi => !pi.GetMethod.IsVirtual))//recorremos las propiedades
                {
                    //buscamos si existe el control
                    Control control = lstControls.FirstOrDefault(cl => cl.Name == "txt" + pi.Name);
                    //Buscamos la descripción de la columna
                    var columnDefinition = lstColumnsDefinitions.FirstOrDefault(cd => cd.column == pi.Name);
                    #region tooltip
                    var controlTooltip = lstControls.FirstOrDefault(cl => cl.Name.EndsWith(pi.Name));
                    if (controlTooltip != null && columnDefinition != null && !string.IsNullOrWhiteSpace(columnDefinition.description))
                    {
                        controlTooltip.ToolTip = columnDefinition.description;
                    }
                    #endregion

                    if (control != null && columnDefinition != null)        //Verifcamos que tengamos un control
                    {
                        TextBox         txt           = control as TextBox; //Convertimos el control a texbox
                        TypeCode        typeCode      = Type.GetTypeCode(Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType);
                        int             maxLengthProp = MaxLengthPropertyClass.GetMaxLength(txt) == 0 ? txt.MaxLength : MaxLengthPropertyClass.GetMaxLength(txt);
                        EnumFormatInput formatInput   = FormatInputPropertyClass.GetFormatInput(txt);//Formato del campo de texto
                        switch (typeCode)
                        {
                            #region String
                        case TypeCode.String:
                        case TypeCode.Char:
                        {
                            txt.MaxLength = (maxLengthProp > 0) ? maxLengthProp : columnDefinition.maxLength;//Asignamos el maxLength
                            if (formatInput == EnumFormatInput.NotSpecialCharacters)
                            {
                                txt.PreviewTextInput += TextBoxHelper.TextInputSpecialCharacters;
                            }
                            break;
                        }
                            #endregion

                            #region Decimal
                        case TypeCode.Decimal:
                        case TypeCode.Double:
                        {
                            //Si permite decimales
                            if (columnDefinition.scale > 0 || PrecisionPropertyClass.GetPrecision(txt) != "0,0")
                            {
                                if (PrecisionPropertyClass.GetPrecision(txt) == "0,0")
                                {
                                    PrecisionPropertyClass.SetPrecision(txt, columnDefinition.precision - columnDefinition.scale + "," + columnDefinition.scale);
                                }
                                txt.PreviewTextInput += TextBoxHelper.DecimalTextInput;
                                txt.PreviewKeyDown   += TextBoxHelper.Decimal_PreviewKeyDown;
                                txt.GotFocus         += TextBoxHelper.DecimalGotFocus;
                                if (enumMode != EnumMode.Search)
                                {
                                    txt.LostFocus += TextBoxHelper.LostFocus;
                                }
                            }
                            //Si sólo permite enteros
                            else
                            {
                                txt.PreviewTextInput += TextBoxHelper.IntTextInput;
                                txt.PreviewKeyDown   += TextBoxHelper.ValidateSpace;
                                if (enumMode != EnumMode.Search)
                                {
                                    txt.LostFocus += TextBoxHelper.LostFocus;
                                    txt.GotFocus  += TextBoxHelper.IntGotFocus;
                                }
                            }
                            txt.MaxLength = (maxLengthProp > 0) ? maxLengthProp : columnDefinition.maxLength;
                            break;
                        }
                            #endregion

                            #region Byte
                        case TypeCode.Byte:
                        {
                            txt.MaxLength         = (maxLengthProp > 0 && maxLengthProp <= 3) ? maxLengthProp : 3;
                            txt.PreviewTextInput += TextBoxHelper.ByteTextInput;
                            txt.PreviewKeyDown   += TextBoxHelper.ValidateSpace;
                            if (enumMode != EnumMode.Search)
                            {
                                txt.LostFocus += TextBoxHelper.LostFocus;
                            }
                            break;
                        }
                            #endregion

                            #region Int
                        case TypeCode.Int16:
                        case TypeCode.Int32:
                        case TypeCode.Int64:
                        {
                            switch (formatInput)
                            {
                            case EnumFormatInput.Number:
                                txt.PreviewTextInput += TextBoxHelper.IntTextInput;
                                txt.MaxLength         = (maxLengthProp > 0) ? maxLengthProp : columnDefinition.maxLength;
                                break;

                            case EnumFormatInput.NumberNegative:
                                txt.PreviewTextInput += TextBoxHelper.IntWithNegativeTextInput;
                                txt.MaxLength         = (maxLengthProp > 0) ? maxLengthProp + 1 : columnDefinition.maxLength + 1;
                                break;
                            }
                            txt.PreviewKeyDown += TextBoxHelper.ValidateSpace;
                            if (enumMode != EnumMode.Search)
                            {
                                txt.LostFocus += TextBoxHelper.LostFocus;
                            }
                            break;
                        }
                            #endregion

                            #region DateTime
                        case TypeCode.DateTime:
                        {
                            if (txt.Name.EndsWith("DT"))
                            {
                                txt.MaxLength = 24;
                            }
                            else if (txt.Name.EndsWith("T"))
                            {
                                txt.MaxLength = 5;
                            }
                            else
                            {
                                txt.MaxLength = 10;
                            }
                            break;
                        }
                            #endregion
                        }
                    }
                }
                #endregion
            }
        }
Exemple #2
0
        /// <summary>
        /// Configura el Estilo de los DataGridTextColumn
        /// Maxlength, Admision de caracteres especiales, Numerico, Decimal , Precision, Escala,
        /// </summary>
        /// <param name="dtgGrid">Grid a configurar</param>
        /// <param name="objBinding">Objeto con el que se quiere configurar el grid</param>
        /// <history>
        /// [emoguel] 28/07/2016  Created.
        /// [erosado] 29/07/2016  Modified. Se agregó el Tag para definir el Maxlength desde la columna.
        /// [emoguel] 13/10/2016 Modified. Se agrego en los strings que puedan recibir unicamente números
        /// </history>
        public static void SetUpGrid <T>(DataGrid dtgGrid, T objBinding,
                                         EnumDatabase database = EnumDatabase.IntelligentMarketing) where T : class
        {
            List <DataGridTextColumn> lstColumns =
                dtgGrid.Columns.Where(dgc => dgc is DataGridTextColumn).OfType <DataGridTextColumn>().ToList();
            List <Model.Classes.ColumnDefinition> lstColumnsDefinitions = BRHelpers.GetFieldsByTable <T>(objBinding, database);

            //Obtenemos la propiedades desde la BD

            #region Object properties

            Type type = objBinding.GetType();
            List <PropertyInfo> lstProperties =
                type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(pi => !pi.GetMethod.IsVirtual).ToList();

            #endregion

            lstColumns.ForEach(dgc =>
            {
                if (!string.IsNullOrWhiteSpace(dgc.SortMemberPath))
                {
                    PropertyInfo property = lstProperties.Where(pi => pi.Name == dgc.SortMemberPath).FirstOrDefault();
                    var columnDefinition  = lstColumnsDefinitions.FirstOrDefault(cd => cd.column == dgc.SortMemberPath);
                    if (columnDefinition != null)
                    {
                        TypeCode typeCode =
                            Type.GetTypeCode(Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);
                        Style style = new Style(typeof(TextBox));
                        EnumFormatInput formatInput = FormatInputPropertyClass.GetFormatInput(dgc); //Formato del campo de texto
                        int maxLengthProp           = MaxLengthPropertyClass.GetMaxLength(dgc);     //Maxlength definido desde la columna
                        switch (typeCode)
                        {
                            #region String

                        case TypeCode.String:
                        case TypeCode.Char:
                            {
                                style.Setters.Add(new Setter(TextBox.MaxLengthProperty,
                                                             (maxLengthProp > 0) ? maxLengthProp : columnDefinition.maxLength)); //Asignamos el maxLength
                                switch (formatInput)
                                {
                                case EnumFormatInput.NotSpecialCharacters: //Bloquea caracteres especiales
                                    {
                                        style.Setters.Add(new EventSetter(UIElement.PreviewTextInputEvent,
                                                                          new TextCompositionEventHandler(TextBoxHelper.TextInputSpecialCharacters)));
                                        break;
                                    }

                                case EnumFormatInput.Number://Cuando un String requiera guardar números enteros
                                    {
                                        style.Setters.Add(new EventSetter()
                                        {
                                            Event   = UIElement.PreviewTextInputEvent,
                                            Handler = new TextCompositionEventHandler(TextBoxHelper.IntTextInput)
                                        });
                                        break;
                                    }
                                }
                                dgc.EditingElementStyle = style;
                                break;
                            }

                            #endregion

                            #region Decimal

                        case TypeCode.Decimal:
                        case TypeCode.Double:
                            {
                                if (columnDefinition.scale > 0 || PrecisionPropertyClass.GetPrecision(dgc) != "0,0")
                                {
                                    style.Setters.Add(new EventSetter(UIElement.PreviewTextInputEvent,
                                                                      new TextCompositionEventHandler(TextBoxHelper.DecimalTextInput))); //Validar texto
                                    var precisionProperty = PrecisionPropertyClass.GetPrecision(dgc);
                                    if (precisionProperty == "0,0")
                                    {
                                        style.Setters.Add(new Setter(PrecisionPropertyClass.PrecisionProperty,
                                                                     columnDefinition.precision - columnDefinition.scale + "," + columnDefinition.scale));
                                        //Agregar Presicion
                                    }
                                    else
                                    {
                                        var precision = precisionProperty.Split(',');
                                        style.Setters.Add(new Setter(MaxLengthPropertyClass.MaxLengthProperty,
                                                                     Convert.ToInt16(precision[0]) + Convert.ToInt16(precision[1]) + 1));
                                        style.Setters.Add(new Setter(PrecisionPropertyClass.PrecisionProperty, precisionProperty));
                                        //Agregar Presicion
                                    }
                                    style.Setters.Add(new EventSetter(UIElement.PreviewKeyDownEvent,
                                                                      new KeyEventHandler(TextBoxHelper.Decimal_PreviewKeyDown))); //Validar espacios en blanco y borrado
                                }
                                else
                                {
                                    style.Setters.Add(new EventSetter(UIElement.PreviewTextInputEvent,
                                                                      new TextCompositionEventHandler(TextBoxHelper.IntTextInput))); //Validar enteros
                                    style.Setters.Add(new EventSetter(UIElement.PreviewKeyDownEvent,
                                                                      new KeyEventHandler(TextBoxHelper.Decimal_PreviewKeyDown)));   //Validar espacios en blanco
                                }
                                style.Setters.Add(new Setter(TextBox.MaxLengthProperty,
                                                             (maxLengthProp > 0) ? maxLengthProp : columnDefinition.maxLength)); //Asignamos el maxLength
                                break;
                            }

                            #endregion

                            #region Int

                        case TypeCode.Int16:
                        case TypeCode.Int32:
                        case TypeCode.Int64:
                            {
                                switch (formatInput)
                                {
                                case EnumFormatInput.Number:
                                    style.Setters.Add(new EventSetter()
                                    {
                                        Event   = UIElement.PreviewTextInputEvent,
                                        Handler = new TextCompositionEventHandler(TextBoxHelper.IntTextInput)
                                    });
                                    style.Setters.Add(new Setter(TextBox.MaxLengthProperty,
                                                                 (maxLengthProp > 0) ? maxLengthProp : columnDefinition.maxLength)); //Asignamos el maxLength
                                    break;

                                case EnumFormatInput.NumberNegative:
                                    style.Setters.Add(new EventSetter()
                                    {
                                        Event   = UIElement.PreviewTextInputEvent,
                                        Handler = new TextCompositionEventHandler(TextBoxHelper.IntWithNegativeTextInput)
                                    });
                                    style.Setters.Add(new Setter(TextBox.MaxLengthProperty,
                                                                 (maxLengthProp > 0) ? maxLengthProp + 1 : columnDefinition.maxLength + 1));
                                    break;
                                }
                                style.Setters.Add(new EventSetter()
                                {
                                    Event   = UIElement.PreviewKeyDownEvent,
                                    Handler = new KeyEventHandler(TextBoxHelper.ValidateSpace)
                                });
                                break;
                            }

                            #endregion

                            #region Byte

                        case TypeCode.Byte:
                            {
                                style.Setters.Add(new Setter(TextBox.MaxLengthProperty,
                                                             (maxLengthProp > 0 && maxLengthProp <= 3) ? maxLengthProp : 3));     //Asignamos el maxLength
                                style.Setters.Add(new EventSetter(UIElement.PreviewTextInputEvent,
                                                                  new TextCompositionEventHandler(TextBoxHelper.ByteTextInput))); //Validar enteros
                                style.Setters.Add(new EventSetter(UIElement.KeyDownEvent,
                                                                  new KeyEventHandler(TextBoxHelper.Decimal_PreviewKeyDown)));    //Validar enteros
                                break;
                            }

                            #endregion
                        }
                        dgc.EditingElementStyle = style;
                    }
                }
            });
        }