private TextBox CreateTextBox <TValue>(FormatProperties formatProperties, Func <string, IFormatProvider, TValue> parse, Func <TValue, string, IFormatProvider, string> toString, string format) { TextBox textBox = new TextBox(); Func <string, TValue> parse2; Func <TValue, string> toString2; if (formatProperties != null) { parse2 = (s) => parse(s, formatProperties.FormatProvider); toString2 = (v) => toString(v, formatProperties.Format ?? format, formatProperties.FormatProvider); } else { parse2 = (s) => parse(s, null); toString2 = (v) => toString(v, format, null); } //TextProperties textProperties = properties.OfType<TextProperties>().FirstOrDefault(); ControlExchange.Set(textBox, new DelegateFormattableTextBoxExchange <TValue>(parse2, toString2)); return(textBox); }
private ComboBox CreateList(Type valueType, IRefList refList, ViewProperties[] properties, IServiceProvider serviceProvider) { ComboBox combo = new ComboBox(); combo.CausesValidation = true; combo.Sorted = false; combo.FormattingEnabled = true; combo.DropDownStyle = ComboBoxStyle.DropDownList; FormatProperties formatProperties = properties.OfType <FormatProperties>().FirstOrDefault(); string format = formatProperties.With(x => x.Format); IFormatProvider formatProvider = formatProperties.With(x => x.FormatProvider); ICustomFormatter customFormatter = formatProperties.With(x => x.CustomFormatter); combo.Format += (sender, args) => { if (!object.Equals(args.DesiredType, typeof(string))) { Log <DefaultEditorFactory> .Error("Se intenta convertir a un valor diferente de String."); args.Value = null; return; } // Se formatea. try { object item = args.ListItem; if (item is IFormattable) { args.Value = ((IFormattable)item).ToString(format, formatProvider); } else if (customFormatter != null) { args.Value = customFormatter.Format(format, item, formatProvider); } else { args.Value = item.ToString(); } } catch (Exception e) { Log <DefaultEditorFactory> .Error("Excepcion al formatear.", e); args.Value = null; } }; ControlExchange.Set(combo, TypeUtils.NewGeneric <ControlExchange>(typeof(ComboBoxExchange <>), valueType)); return(combo); }
public static void Set(Control control, ControlExchange exchange) { control.Tag = exchange; }