Exemple #1
0
        private static ParameterValueEditor EditorWithSingleSelect(ParameterInfo parameterInfo)
        {
            // Выбор одного значения из предопределенного списка

            var parameterValueControl = EditorFactory.CreateObjectEditor(parameterInfo.Type);
            var selectValueDlg        = new DialogView <SelectSingleValueView>();

            var editor = new ParameterValueEditor(parameterValueControl)
            {
                ShowNullButton   = false,
                ShowSelectButton = true
            };

            editor.ChangingValue += (sender, args) =>
            {
                // Устанавливаем значение, только если оно есть в списке

                object newValue = null;
                string newLabel = null;

                if (editor.AvailableValues.IsNullOrEmpty() == false)
                {
                    var selectedValue = parameterValueControl.CastObjectValue(ValueOrFirstItem(args.Value));

                    if (selectedValue != null)
                    {
                        foreach (var item in editor.AvailableValues)
                        {
                            if (Equals(item.Value, selectedValue))
                            {
                                newValue = item.Value;
                                newLabel = item.Key;
                                break;
                            }
                        }
                    }
                }

                args.Value = newValue;
                args.Label = newLabel;
            };

            editor.SelectValue += (sender, args) =>
            {
                // Показываем диалог, только если есть доступные значения

                args.Cancel = true;

                if (editor.AvailableValues.IsNullOrEmpty() == false)
                {
                    selectValueDlg.View.AvailableValues = editor.AvailableValues;
                    selectValueDlg.View.SelectedValue   = editor.Value;

                    if (selectValueDlg.ShowDialog() == DialogResult.OK)
                    {
                        args.Value  = selectValueDlg.View.SelectedValue;
                        args.Cancel = false;
                    }
                }
            };

            return(editor);
        }