Beispiel #1
0
        /// <summary>
        /// Updates the list control based on the given property change.
        /// Updates the list of possible items based on the values provided by the property
        /// and inserts a blank item for a drop down list if the property is editable and not required.
        /// </summary>
        /// <param name="change">The property change.</param>
        protected override void UpdateElement(PropertyChange change)
        {
            // let the base handle everything except value change
            base.UpdateElement(change - PropertyChange.Value);
            ListControl list = control as ListControl;

            if (property == null || list == null)
            {
                return;
            }

            object  val        = property.InternalValue;
            IList   lst        = val == null ? new ArrayList() : val as IList;
            ListBox lb         = control as ListBox;
            bool    isMultiVal = lst != null && (control is CheckBoxList ||
                                                 lb != null && lb.SelectionMode == ListSelectionMode.Multiple);
            DropDownList dl = control as DropDownList;

            if (change.IncludesItems() || // if items list changed,
                isMultiVal && change.IncludesEditable() || // or control shows all items and Editable changed,
                dl != null && change.IncludesRequired() || // or control is DropDownList and Required changed,
                change.IncludesValue())    // or property value changed
            {
                // get the list of items to show
                IEnumerable src = null;
                if (isMultiVal && !property.Editable)
                {
                    src = lst;
                }
                else if (property.ItemsProvider != null)
                {
                    src = property.ItemsProvider(null);
                }

                list.Items.Clear(); // clear control's item collection

                // for DropDownList, add special items in case when not Required or Required and has no value
                if (dl != null && !property.Required)
                {
                    list.Items.Add(new ListItem("", property.NullString));
                }
                if (dl != null && property.Required && property.IsNull())
                {
                    list.Items.Add(new ListItem("Select Value...", ""));
                }
                int insertionPoint = list.Items.Count;

                // add items to show
                if (src != null)
                {
                    foreach (object i in src)
                    {
                        ListItem li = new ListItem(property.ValueToString(i, ValueFormat.DisplayString),
                                                   property.ValueToString(i, ValueFormat.EditString));
                        list.Items.Add(li);
                        li.Selected = src == lst; // if items are values, mark them as selected
                    }
                }

                // if not showing only values, make sure the values are present in the list and mark them as selected
                if (src != lst)
                {
                    IEnumerable values = isMultiVal ? lst : new object[] { (lst != null && lst.Count > 0) ? lst[0] : property.InternalValue };
                    foreach (object i in values)
                    {
                        ListItem li = list.Items.FindByValue(property.ValueToString(i, ValueFormat.EditString));
                        if (li == null) // add values not in list
                        {
                            li = new ListItem(property.ValueToString(i, ValueFormat.DisplayString),
                                              property.ValueToString(i, ValueFormat.EditString));
                            li.Attributes["disabled"] = "disabled";; // disable items not included in the list of expected values
                            list.Items.Insert(insertionPoint++, li); // insert them after special items and before expected values
                        }
                        li.Selected = true;
                    }
                }
            }
        }
        /// <summary>
        /// Updates the selector based on the given property change.
        /// Updates the list of possible items based on the values provided by the property
        /// and inserts a blank item for a combo box if the property is editable and not required.
        /// </summary>
        /// <param name="change">The property change.</param>
        protected override void UpdateElement(PropertyChange change)
        {
            base.UpdateElement(change);
            Selector sel = element as Selector;

            if (property == null || sel == null) return;

            object val = property.InternalValue;
            IList lst = val as IList;
            ListBox lb = element as ListBox;
            ComboBox cb = element as ComboBox;

            if (change.IncludesItems() ||
                lb != null && change.IncludesEditable() ||
                cb != null && change.IncludesRequired())
            {
                sel.Items.Clear();
                IEnumerable src = null;
                if (lb != null && !property.Editable && lst != null) src = lst;
                else if (property.ItemsProvider != null) src = property.ItemsProvider(null);

                // for non-required drop down lists add null string option
                if (cb != null && !cb.IsEditable && !property.Required)
                    sel.Items.Add(property.NullString);

                // add items explicitly. Don't use sel.ItemSource since src is not observable
                if (src != null) foreach (object item in src) sel.Items.Add(item);
            }
            if (change.IncludesValue() || change.IncludesItems())
            {
                if (lb != null && lst != null && lb.SelectionMode != SelectionMode.Single)
                {
                    lb.SelectedItems.Clear();
                    foreach (object item in lst) lb.SelectedItems.Add(item);
                }
                else if (lst != null && lst.Count > 0) sel.SelectedItem = lst[0];
                else sel.SelectedItem = val;

                if (cb != null && cb.IsEditable) cb.Text = property.EditStringValue;
            }
        }
        /// <summary>
        /// Updates the list control based on the given property change.
        /// Updates the list of possible items based on the values provided by the property
        /// and inserts a blank item for a drop down list if the property is editable and not required.
        /// </summary>
        /// <param name="change">The property change.</param>
        protected override void UpdateElement(PropertyChange change)
        {
            // let the base handle everything except value change
            base.UpdateElement(change - PropertyChange.Value);
            ListControl list = control as ListControl;

            if (property == null || list == null) return;

            object val = property.InternalValue;
            IList lst = val == null ? new ArrayList() : val as IList;
            ListBox lb = control as ListBox;
            bool isMultiVal = lst != null && (control is CheckBoxList
                || lb != null && lb.SelectionMode == ListSelectionMode.Multiple);
            DropDownList dl = control as DropDownList;

            if (change.IncludesItems() // if items list changed,
                || isMultiVal && change.IncludesEditable() // or control shows all items and Editable changed,
                || dl != null && change.IncludesRequired() // or control is DropDownList and Required changed,
                || change.IncludesValue()) // or property value changed
            {
                // get the list of items to show
                IEnumerable src = null;
                if (isMultiVal && !property.Editable) src = lst;
                else if (property.ItemsProvider != null) src = property.ItemsProvider(null);

                list.Items.Clear(); // clear control's item collection

                // for DropDownList, add special items in case when not Required or Required and has no value
                if (dl != null && !property.Required)
                    list.Items.Add(new ListItem("", property.NullString));
                if (dl != null && property.Required && property.IsNull())
                    list.Items.Add(new ListItem("Select Value...", ""));
                int insertionPoint = list.Items.Count;

                // add items to show
                if (src != null)
                {
                    foreach (object i in src)
                    {
                        ListItem li = new ListItem(property.ValueToString(i, ValueFormat.DisplayString),
                            property.ValueToString(i, ValueFormat.EditString));
                        list.Items.Add(li);
                        li.Selected = src == lst; // if items are values, mark them as selected
                    }
                }

                // if not showing only values, make sure the values are present in the list and mark them as selected
                if (src != lst)
                {
                    IEnumerable values = isMultiVal ? lst : new object[] { (lst != null && lst.Count > 0) ? lst[0] : property.InternalValue };
                    foreach (object i in values)
                    {
                        ListItem li = list.Items.FindByValue(property.ValueToString(i, ValueFormat.EditString));
                        if (li == null) // add values not in list
                        {
                            li = new ListItem(property.ValueToString(i, ValueFormat.DisplayString),
                                property.ValueToString(i, ValueFormat.EditString));
                            li.Attributes["disabled"] = "disabled"; ; // disable items not included in the list of expected values
                            list.Items.Insert(insertionPoint++, li); // insert them after special items and before expected values
                        }
                        li.Selected = true;
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Updates the selector based on the given property change.
        /// Updates the list of possible items based on the values provided by the property
        /// and inserts a blank item for a combo box if the property is editable and not required.
        /// </summary>
        /// <param name="change">The property change.</param>
        protected override void UpdateElement(PropertyChange change)
        {
            base.UpdateElement(change);
            Selector sel = element as Selector;

            if (property == null || sel == null)
            {
                return;
            }

            object   val = property.InternalValue;
            IList    lst = val as IList;
            ListBox  lb  = element as ListBox;
            ComboBox cb  = element as ComboBox;

            if (change.IncludesItems() ||
                lb != null && change.IncludesEditable() ||
                cb != null && change.IncludesRequired())
            {
                sel.Items.Clear();
                IEnumerable src = null;
                if (lb != null && !property.Editable && lst != null)
                {
                    src = lst;
                }
                else if (property.ItemsProvider != null)
                {
                    src = property.ItemsProvider(null);
                }

                // for non-required drop down lists add null string option
                if (cb != null && !cb.IsEditable && !property.Required)
                {
                    sel.Items.Add(property.NullString);
                }

                // add items explicitly. Don't use sel.ItemSource since src is not observable
                if (src != null)
                {
                    foreach (object item in src)
                    {
                        sel.Items.Add(item);
                    }
                }
            }
            if (change.IncludesValue() || change.IncludesItems())
            {
                if (lb != null && lst != null && lb.SelectionMode != SelectionMode.Single)
                {
                    lb.SelectedItems.Clear();
                    foreach (object item in lst)
                    {
                        lb.SelectedItems.Add(item);
                    }
                }
                else if (lst != null && lst.Count > 0)
                {
                    sel.SelectedItem = lst[0];
                }
                else
                {
                    sel.SelectedItem = val;
                }

                if (cb != null && cb.IsEditable)
                {
                    cb.Text = property.EditStringValue;
                }
            }
        }