Пример #1
0
        public void ImportFromModel(object model, params object[] context)
        {
            TValue input = default(TValue);

            if (model != null)
            {
                input = (TValue)model;
            }


            ChoiceList <TChoiceItem, TValue, TDisplay> choiceList = context[0] as ChoiceList <TChoiceItem, TValue, TDisplay>;

            List <MutualExclusionListItem <TValue> > items = new List <MutualExclusionListItem <TValue> >();


            foreach (TChoiceItem item in choiceList.Items)
            {
                MutualExclusionListItem <TValue> mutualExclusionListItem = new MutualExclusionListItem <TValue>();

                mutualExclusionListItem.Value = choiceList.ValueSelector(item);
                mutualExclusionListItem.Label = choiceList.DisplaySelector(item).ToString();

                if (choiceList.LabelAttributesSelector != null)
                {
                    mutualExclusionListItem.LabelAttributes = choiceList.LabelAttributesSelector(item);
                }
                else
                {
                    mutualExclusionListItem.LabelAttributes = new { }
                };
                if (choiceList.DisplayAttributesSelector != null)
                {
                    mutualExclusionListItem.DisplayAttributes = choiceList.DisplayAttributesSelector(item);
                }
                else
                {
                    mutualExclusionListItem.DisplayAttributes = new { }
                };
                items.Add(mutualExclusionListItem);

                if (((IComparable)input).CompareTo(mutualExclusionListItem.Value) == 0)
                {
                    mutualExclusionListItem.Selected = true;
                }
            }
            Items = items.ToArray();
        }
    }
Пример #2
0
        private static List <ExtendedSelectListItem> prepareItems <TItem, TDisplay, TValue>(ChoiceList <TItem, TDisplay, TValue> choices, bool isDropDown, ModelMetadata model)
        {
            List <ExtendedSelectListItem> res = new List <ExtendedSelectListItem>();

            if (choices.isInClientBlock)
            {
                return(res);
            }
            if (isDropDown && (choices.UsePrompt || choices.OverridePrompt != null))
            {
                string prompt = choices.OverridePrompt;
                if (prompt == null)
                {
                    prompt = model.Watermark;
                }
                if (prompt != null)
                {
                    res.Add(new ExtendedSelectListItem
                    {
                        Value      = string.Empty,
                        Text       = prompt,
                        Attributes =
                            choices.LabelAttributesSelector == null ?
                            null : choices.LabelAttributesSelector(default(TItem))
                    });
                }
            }
            foreach (TItem item in choices.Items)
            {
                res.Add(new ExtendedSelectListItem
                {
                    Attributes =
                        choices.LabelAttributesSelector == null ?
                        null : choices.LabelAttributesSelector(item),
                    Value           = Convert.ToString(choices.ValueSelector(item), CultureInfo.CurrentCulture),
                    Text            = Convert.ToString(choices.DisplaySelector(item), CultureInfo.CurrentCulture),
                    GroupKey        = choices is IGroupedChoiceList ? Convert.ToString(((IGroupedChoiceList)choices).GroupValueSelector(item), CultureInfo.CurrentCulture) : null,
                    GroupName       = choices is IGroupedChoiceList ? Convert.ToString(((IGroupedChoiceList)choices).GroupDisplaySelector(item), CultureInfo.CurrentCulture):null,
                    GroupAttributes = choices is IGroupedChoiceList && ((IGroupedChoiceList)choices).GroupAttributesSelector != null ?
                                      ((IGroupedChoiceList)choices).GroupAttributesSelector(item) : null
                });
            }
            return(res);
        }
Пример #3
0
        public void ImportFromModel(object model, params object[] context)
        {
            IEnumerable   input = model as IEnumerable;
            List <TValue> inputList;

            if (input == null)
            {
                inputList = new List <TValue>();
            }
            else
            {
                inputList = EnumerableHelper.CreateFrom <TValue>(typeof(List <TValue>), input, 0) as List <TValue>;

                inputList.Sort();

                ChoiceList <TChoiceItem, TValue, TDisplay> choiceList = context[0] as ChoiceList <TChoiceItem, TValue, TDisplay>;

                List <CheckBoxListItem <TValue> > items   = new List <CheckBoxListItem <TValue> >();
                List <OrderItem <TValue> >        orderer = new List <OrderItem <TValue> >();

                int index = 0;

                foreach (TChoiceItem item in choiceList.Items)
                {
                    CheckBoxListItem <TValue> checkBoxListItem = new CheckBoxListItem <TValue>();
                    OrderItem <TValue>        orderItem        = new OrderItem <TValue>();
                    checkBoxListItem.Code  = choiceList.ValueSelector(item);
                    checkBoxListItem.Label = choiceList.DisplaySelector(item).ToString();

                    if (choiceList.LabelAttributesSelector != null)
                    {
                        checkBoxListItem.LabelAttributes = choiceList.LabelAttributesSelector(item);
                    }
                    else
                    {
                        checkBoxListItem.LabelAttributes = new { }
                    };
                    if (choiceList.DisplayAttributesSelector != null)
                    {
                        checkBoxListItem.DisplayAttributes = choiceList.DisplayAttributesSelector(item);
                    }
                    else
                    {
                        checkBoxListItem.DisplayAttributes = new { }
                    };
                    items.Add(checkBoxListItem);

                    orderItem.Value = checkBoxListItem.Code;
                    orderItem.Place = index;
                    orderer.Add(orderItem);
                    index++;
                }
                Items = items.ToArray();
                orderer.Sort();
                IEnumerator <OrderItem <TValue> > ordererIterator = (orderer as IEnumerable <OrderItem <TValue> >).GetEnumerator();
                ordererIterator.Reset();
                ordererIterator.MoveNext();
                foreach (TValue selectedValue in inputList)
                {
                    while (((IComparable)selectedValue).CompareTo(ordererIterator.Current.Value) > 0)
                    {
                        ordererIterator.MoveNext();
                    }
                    if (((IComparable)selectedValue).CompareTo(ordererIterator.Current.Value) == 0)
                    {
                        Items[ordererIterator.Current.Place].Selected = true;
                    }
                }
            }
        }
    }