public static IPresenter GetPresenter(this IParameterDescriptor param)
 {
     if (PresenterProperty.TryGet(param.Metadata, out var presenter))
     {
         return(presenter);
     }
     if (TryGetPresentTypeConverter(param, out _))
     {
         return(TypeConvertedPresenter.Instance);
     }
     if (param.IsSelectable())
     {
         return(SelectablePresenter.Instance);
     }
     if (param.IsMultiValue())
     {
         return(MultiValuePresenter.Instance);
     }
     return(GetPresenter(param.ValueType) ?? throw new NotSupportedException($"presenter not found for type '{param.ValueType}'"));
 }
Exemple #2
0
        public PresentedParameter Present(IParameterDescriptor param, Action updateCallback)
        {
            string ToStrFunc(object value) => param.ConvertValueToString(value);

            IEnumerable items;

            if (SelectableValuesFuncProperty.TryGet(param.Metadata, out var selectableValuesFunc))
            {
                items = selectableValuesFunc(param);
            }
            else if (param.IsSelectable())
            {
                items = param.SelectableValues;
            }
            else
            {
                throw new ProgrammingException("Parameter.SelectableValues or SelectablePresenter.SelectableValuesFuncProperty must be assigned");
            }

            var comboBox = new ComboBox {
                ItemsSource = ToStringOverridenWrapper.Of(items, ToStrFunc)
            };

            comboBox.SelectionChanged += (sender, args) => updateCallback();
            void Setter(object val) => comboBox.SelectedValue = ToStringOverridenWrapper.Wrap(val, ToStrFunc);
            object Getter() => ToStringOverridenWrapper.TryUnwrap(comboBox.SelectedValue);

            void Updater(ParameterStateType state, bool value)
            {
                if (state == ParameterStateType.Enabled)
                {
                    comboBox.IsEnabled = value;
                }
            }

            return(new PresentedParameter(param, comboBox, new PresentedParameter.ParamDelegates(Getter, Setter, value => value != null, Updater)));
        }