Esempio n. 1
0
        /// <summary>
        /// Fill list box with fileds of the current datasource.
        /// </summary>
        /// <param name="listBox">ListBox control to fill.</param>
        /// <param name="context">Type descriptor context.</param>
        /// <param name="value">Current value.</param>
        protected override void FillListBox(ListBox listBox, ITypeDescriptorContext context, object value)
        {
            var selectedField = (string)value;

            if (selectedField == null)
            {
                selectedField = string.Empty;
            }

            var dataSourceDescriptor = TypeDescriptor.GetProperties(context.Instance)["DataSource"];

            if (dataSourceDescriptor == null)
            {
                return;
            }
            var dataSource = dataSourceDescriptor.GetValue(context.Instance);

            if (dataSource != null)
            {
                var currencyManager = new BindingContext()[dataSource] as CurrencyManager;
                if (currencyManager != null)
                {
                    foreach (PropertyDescriptor descriptor in currencyManager.GetItemProperties())
                    {
                        int lastIndex = listBox.Items.Add(descriptor.Name);
                        if (string.Compare(descriptor.Name, selectedField) == 0)
                        {
                            listBox.SelectedIndex = lastIndex;
                        }
                    }
                }
            }
        }