using CloudBuilder; using CloudBuilder.Gui; using CloudBuilder.Gui.DependencyInjection; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Krypton.Toolkit { public static class KryptonListBoxExtensions { public static ExFormSnapshotEntry GetExFormSnapshotEntry(this KryptonListBox kryptonListBox) { string bindDataName = null; if (GetProvider().GetCustomAttribute(kryptonListBox) != null) bindDataName = GetProvider().GetCustomAttribute(kryptonListBox).BindDataName; return new ExFormSnapshotEntry( kryptonListBox.Name, bindDataName, kryptonListBox.GetAllItemValue(), kryptonListBox.Text); } public static void Set(this KryptonListBox kryptonListBox, BindingContextCustomAttribute bindingContext) { GetProvider().SetCustomAttribute(kryptonListBox, bindingContext); } public static void Set(this KryptonListBox kryptonListBox, EnumEditModeColor editModeColor) { GetProvider().SetCustomAttribute(kryptonListBox, editModeColor); } public static void Set(this KryptonListBox kryptonListBox, string bindDataName, EnumEditModeColor editModeColor) { GetProvider().SetCustomAttribute(kryptonListBox, bindDataName, editModeColor); } public static void Set(this KryptonListBox kryptonListBox, string bindDataName) { GetProvider().SetCustomAttribute(kryptonListBox, bindDataName, EnumBindDirection.Both); } public static void Set(this KryptonListBox kryptonListBox, string bindDataName, EnumBindDirection bindDirection) { GetProvider().SetCustomAttribute(kryptonListBox, bindDataName, bindDirection); } public static void Set(this KryptonListBox kryptonListBox, string bindDataName, EnumBindDirection bindDirection, EnumEditModeColor editModeColor) { GetProvider().SetCustomAttribute(kryptonListBox, bindDataName, bindDirection, editModeColor); } public static void Set(this KryptonListBox kryptonListBox, string bindDataName, string bindFormat, EnumBindDirection bindDirection) { GetProvider().SetCustomAttribute(kryptonListBox, bindDataName, bindFormat, bindDirection); } public static void Set(this KryptonListBox kryptonListBox, string permissionName, Operate permissionMode) { GetProvider().SetCustomAttribute(kryptonListBox, permissionName, permissionMode); } public static void Set(this KryptonListBox kryptonListBox, string permissionName, string dependentDetailName, Operate permissionMode) { GetProvider().SetCustomAttribute(kryptonListBox, permissionName, dependentDetailName, permissionMode); } public static void Set(this KryptonListBox kryptonListBox, string permissionName, string dependentDetailName) { GetProvider().SetCustomAttribute(kryptonListBox, permissionName, dependentDetailName); } public static string[] GetAllItemText(this KryptonListBox kryptonListBox) { List l = new List(); for (int i = 0; i < kryptonListBox.Items.Count; i++) { l.Add(((KryptonListItem)kryptonListBox.Items[i]).ShortText); } return l.ToArray(); } public static T[] GetAllItemValue(this KryptonListBox kryptonListBox) { List l = new List(); for (int i = 0; i < kryptonListBox.Items.Count; i++) { l.Add((T)((Dictionary)((KryptonListItem)kryptonListBox.Items[i]).Tag)[i]); } return l.ToArray(); } public static void SetValue(this KryptonListBox kryptonListBox, string[] ts) { kryptonListBox.Items.Clear(); if (ts == null || ts.Length == 0) return; List l = new List(); KryptonListItem kryptonListItem; int i = 0; Dictionary keyValues = new Dictionary(); foreach (string item in ts) { kryptonListItem = new KryptonListItem(); kryptonListItem.ShortText = item; l.Add(kryptonListItem); keyValues.Add(i, item); kryptonListItem.Tag = keyValues; i++; } kryptonListBox.Items.AddRange(l.ToArray()); } public static void SetDataSource(this KryptonListBox kryptonListBox, T[] dataSource, string displayName, string valueName) { if (dataSource == null || dataSource.Length == 0) return; KryptonListItem[] items = KryptonListItemConvertor.ToKryptonListItem(dataSource, displayName, valueName); kryptonListBox.Items.AddRange(items); } public static BindingContextCustomAttribute GetCustomAttribute(this KryptonListBox kryptonListBox) { return GetProvider().GetCustomAttribute(kryptonListBox); } private static BindingContextAttributeProvider GetProvider() { return ServiceHelper.GetService() ?? throw new InvalidOperationException("BindingContextAttributeProvider is not registered in the service GetProvider()."); } } }