Пример #1
0
        protected IEnumerable <IBaseModel> LoadModel(params IListParameter[] parameters)
        {
            _cancellationTokenSource = new CancellationTokenSource();

            //var ret = TaskEx.Run(() => AutoCompleteDataManager.Get<IBaseModel>(parameters), _cancellationTokenSource.Token);
            if (AutoCompleteDataManager == null)
            {
                return(null);
            }
            var ret = AutoCompleteDataManager.Get <IBaseModel>(parameters);

            _cancellationTokenSource = null;

            return(ret);
        }
Пример #2
0
        protected override void OnLoaded()
        {
            base.OnLoaded();

            AppearanceReadOnly.TextOptions.HAlignment = HorzAlignment.Near;

            _autocompleteCache = new Dictionary <int, string>();

            ButtonClick += (sender, args) =>
            {
                var dLookup = sender as dLookupF;
                if (dLookup != null && dLookup.Enabled)
                {
                    dLookup.OpenLookupPopup();
                }
            };

            CustomDisplayText += (sender, args) =>
            {
                int output;
                if (args.Value != null && int.TryParse(args.Value.ToString(), out output))
                {
                    var model = AutoCompleteDataManager.GetFirst <IBaseModel>(WhereTerm.Default(output, "id"));

                    args.DisplayText = model != null?AutoCompleteDisplayFormater(model) : string.Empty;

                    _autocompleteCache.Clear();
                    _autocompleteCache.Add(output, args.DisplayText);
                }
            };

            ParseEditValue += (sender, args) =>
            {
                if (_parsingEditValue)
                {
                    return;
                }

                if (_skipNextEditValueParsing)
                {
                    _skipNextEditValueParsing = false;
                    return;
                }

                _parsingEditValue = true;

                int output;
                var lookup = sender as dLookupF;
                if (lookup == null)
                {
                    return;
                }

                if (args.Value != null && int.TryParse(args.Value.ToString(), out output))
                {
                    if (!_autocompleteCache.ContainsKey(output))
                    {
                        lookup.DefaultValue    = new IListParameter[] { WhereTerm.Default(output, "id") };
                        args.Value             = lookup.DisplayString;
                        _previousDisplayString = lookup.DisplayString;

                        _autocompleteCache.Add(output, lookup.DisplayString);
                    }
                    else
                    {
                        lookup.Value = output;
                        args.Value   = _autocompleteCache[output];
                    }
                }
                else if (lookup.Value != null && int.TryParse(lookup.Value.ToString(), out output))
                {
                    if (args.Value != null && !args.Value.Equals(_previousDisplayString))
                    {
                        _skipNextEditValueParsing = true;
                    }

                    args.Value = output;
                }
                _skipNextEditValueParsing = true;

                args.Handled = true;

                _parsingEditValue = false;
            };

            FormatEditValue += (sender, args) =>
            {
                if (_formattingEditValue)
                {
                    return;
                }

                _formattingEditValue = true;

                int output;
                if (args.Value != null && int.TryParse(args.Value.ToString(), out output))
                {
                    if (!_autocompleteCache.ContainsKey(output))
                    {
                        var model = AutoCompleteDataManager.GetFirst <IBaseModel>(WhereTerm.Default(output, "id"));

                        args.Value = model != null?AutoCompleteDisplayFormater(model) : string.Empty;

                        _autocompleteCache.Add(output, args.Value.ToString());
                    }
                    else
                    {
                        args.Value = _autocompleteCache[output];
                    }
                }

                args.Handled = true;

                _formattingEditValue = false;
            };
        }
Пример #3
0
        private void AutoComplete()
        {
            // ReSharper disable once TooWideLocalVariableScope
            int count;

            if (!ClearOnLeave)
            {
                return;
            }
            if (Text.Length < OriginalTextStartPosition)
            {
                OriginalTextStartPosition = 0;
            }

            var strlen = Text.Length - OriginalTextStartPosition - SelectionLength;

            if (strlen > Text.Length - OriginalTextStartPosition || strlen < 0)
            {
                strlen = Text.Length - OriginalTextStartPosition;
            }

            var searchTerm = Text.Substring(OriginalTextStartPosition, strlen);

            if (!searchTerm.IsEmpty() && !Array.Exists(AutoCompleteCachedSource, s => s.Contains(searchTerm)) &&
                searchTerm.Length > AutocompleteMinimumTextLength)
            {
                IQueryable <IBaseModel> result = null;

                if (AutoCompleteWheretermFormater != null)
                {
                    result = AutoCompleteDataManager
                             .Get <IBaseModel>(Paging.Instance(0, 1), out count, AutoCompleteWheretermFormater(searchTerm))
                             .AsQueryable();
                }
                else if (AutoCompleteWhereExpression != null)
                {
                    if (!(AutoCompleteDataManager is IExtendedQueryableDataManager))
                    {
                        throw new Exception(string.Format("Autocomplete field {0} is using AutoCompleteWhereExpression property, but the AutoCompleteDataManager property does not implement IExtendedQueryableDataManager.", Name));
                    }

                    var param = new AutoCompleteWhereExpressionParam();
                    AutoCompleteWhereExpression(searchTerm, param);

                    result = ((IExtendedQueryableDataManager)AutoCompleteDataManager)
                             .Get <IBaseModel>(Paging.Instance(0, 1), out count, param.Expression, param.Parameters)
                             .AsQueryable();
                }

                if (result != null && result.Any())
                {
                    Value         = (int)result.Select(model => model.Id).ToArray().GetValue(0);
                    DisplayString = result.Select(AutoCompleteDisplayFormater).ToArray().GetValue(0).ToString();

                    ByAuto = true;
                    var strtemp = searchTerm;
                    if (AutoCompleteText != null)
                    {
                        Text = result.Select(AutoCompleteText).ToArray().GetValue(0).ToString();
                    }
                    else
                    {
                        Text = DisplayString;
                    }

                    OriginalTextStartPosition = Text.ToLower().IndexOf(strtemp.ToLower(), StringComparison.Ordinal);
                    var startSel = OriginalTextStartPosition + strtemp.Length;
                    SelectionStart  = startSel;
                    SelectionLength = Text.Length - startSel;

                    OriginalTextStartPosition = OriginalTextStartPosition < 0 ? 0 : OriginalTextStartPosition;

                    DisplayString = Text;
                }
                else
                {
                    Value         = null;
                    DisplayString = string.Empty;
                }
            }

            if (Text.IsEmpty())
            {
                Value         = null;
                DisplayString = string.Empty;
            }
        }