Beispiel #1
0
        public TextField(PropertyReference <T> bindProp, string?name, PropConverter <T, string>?conv = null, bool mono = true)
        {
            _bindProp          = bindProp ?? throw new ArgumentNullException(nameof(bindProp));
            (_getter, _setter) = bindProp.GetAccessors();
            name ??= bindProp.DisplayName;
            if (conv?.NameFormat != null)
            {
                name = string.Format(CultureInfo.InvariantCulture, conv.NameFormat, name);
            }
            _converter = conv ?? PropConverter.Default <T, string>();

            Ui.Drawn += Update;

            name ??= "";

            SizeLayout = new Layout2d("100%", "24");
            NameLabel  = new Label(name)
            {
                PositionLayout = new Layout2d("0", "3")
            };
            NameLabel.CeilSize();
            Add(NameLabel, "lblName");

            Field = new EditBox
            {
                PositionLayout = new Layout2d("lblName.right + 5", "(&.h - h) / 2"),
                SizeLayout     = new Layout2d("100% - x", "22")
            };
            Add(Field);
            if (mono)
            {
                Field.Renderer.Font = Ui.FontMono;
            }

            if (_bindProp.Property is PropertyInfo pi &&
                _bindProp.Target is BaseObject o &&
                o.IsBound(pi.GetGetMethod() !, out var res) &&
                res.Item1 is UserBinding ub)
            {
                _binding   = ub;
                Field.Text = _binding.Code;
            }

            if (_setter == null)
            {
                Field.ReadOnly = true;
            }

            void Validate()
            {
                try
                {
                    Value = Field.Text = Field.Text.Trim();
                }
                catch
                {
                    //
                }
            }

            Field.Unfocused += (sender, args) => { Validate(); };

            Field.ReturnKeyPressed += (sender, s) => { Validate(); };

            /*if (!multiline)
             * {
             *  Field.TextChanged += (sender, s) =>
             *  {
             *      if (s.Value[^1] == '\n')
             *          Validate();
             *  };
             * }*/
        }
Beispiel #2
0
 public PropConverter <TOrig, TOut> Then <TOut>(PropConverter <TDisp, TOut> next)
 {
     return(new PropConverter <TOrig, TOut>(o => next.Get(Get(o)),
                                            (value, old) => Set(next.Set(value, default !), old)));