123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using InABox.Clients;
- using InABox.Core;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using ComboItemType = System.Tuple<string, System.Collections.Generic.Dictionary<string, object?>>;
- namespace InABox.DynamicGrid
- {
- public class DFLookupControl : DynamicFormFieldControl<DFLayoutLookupField, DFLayoutLookupFieldProperties, Guid>
- {
- private DFLayoutLookupValue _value = null!;
-
- private ComboBox Combo = null!; // late-initialising
-
- protected override FrameworkElement Create()
- {
- _value = new DFLayoutLookupValue();
- Combo = new ComboBox();
- if (CoreUtils.TryGetEntity(Field.Properties.LookupType, out var type))
- {
- var client = ClientFactory.CreateClient(type);
- //Task.Run(() =>
- //{
- var columns = LookupFactory.DefineColumns(type);
- foreach(var property in Field.Properties.AdditionalPropertiesList)
- {
- columns.Add(property);
- }
- IFilter? filter;
- if (!Field.Properties.Filter.IsNullOrWhiteSpace())
- {
- filter = Serialization.Deserialize(typeof(Filter<>).MakeGenericType(type), Field.Properties.Filter) as IFilter;
- filter?.ConvertCustomValues((_, value) =>
- {
- var property = System.Text.Encoding.UTF8.GetString(value.Data);
- return FormDesignGrid.DataModel?.GetEntityValue(property);
- });
- }
- else
- {
- filter = LookupFactory.DefineFilter(type);
- }
- var table = client.Query(
- filter,
- columns,
- LookupFactory.DefineSort(type)
- );
- var lookups = new Dictionary<Guid, ComboItemType>()
- {
- { Guid.Empty, new("(None)", table.NewRow().ToDictionary(new[] { "ID" })) }
- };
- foreach (var row in table.Rows)
- {
- var id = row.Get<Guid>("ID");
- var values = row.ToDictionary(new[] { "ID" });
- var display = LookupFactory.FormatLookup(type, values, new string[] { });
- lookups[id] = new(display, values);
- }
- //Dispatcher.Invoke(() =>
- //{
- Combo.DisplayMemberPath = "Value.Item1";
- Combo.SelectedValuePath = "Key";
- Combo.ItemsSource = lookups;
- Combo.VerticalContentAlignment = VerticalAlignment.Center;
- Combo.SelectionChanged += (sender, e) => ChangeField();
- //});
- //});
- }
- return Combo;
- }
- public override void Deserialize(string serialized)
- {
- _value.Load(serialized);
- }
- public override string Serialize()
- {
- return _value.ToString();
- }
- public override object? GetEntityValue()
- {
- return (Guid?)Combo.SelectedValue ?? Guid.Empty;
- }
- public override void SetEntityValue(object? value)
- {
- Combo.SelectedValue = value;
- }
- public override object? GetData(string dataField)
- {
- return ((KeyValuePair<Guid, ComboItemType>?)Combo.SelectedItem)?.Value.Item2.GetValueOrDefault(dataField);
- }
- public override Dictionary<string, object?>? GetAdditionalValues()
- {
- if (Field.Properties.AdditionalPropertiesList.Count == 0)
- return null;
- var values = new Dictionary<string, object?>();
- foreach(var field in Field.Properties.AdditionalPropertiesList)
- {
- values[field] = GetData(field);
- }
- return values;
- }
- public override Guid GetValue() => ((KeyValuePair<Guid, ComboItemType>?)Combo.SelectedItem)?.Key ?? Guid.Empty;
- public override void SetValue(Guid value)
- {
- var lookups = Combo.ItemsSource as Dictionary<Guid, ComboItemType>;
- if (lookups is null)
- return;
- var lookup = lookups!.FirstOrDefault(x => x.Key == value);
- Combo.SelectedItem = lookup;
- }
- protected override bool IsEmpty() => Combo.SelectedValue == null || (Guid)Combo.SelectedValue == Guid.Empty;
- }
- }
|