| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | using System;using System.Collections.Generic;using System.Linq;using System.Windows;using System.Windows.Controls;using System.Windows.Media;using InABox.Clients;using InABox.Core;namespace InABox.DynamicGrid{    public class PopupEditorControl : DynamicEditorControl<Guid>, IPopupEditorControl    {        private Type? _type;        private Guid _value = Guid.Empty;        private TextBox Editor;        //public event DefineFilter OnDefineFilter;        public PopupEditorControl()        {            OtherColumns = new Dictionary<string, string>();        }        public Dictionary<string, string> OtherColumns { get; }        public event OnDefineFilter? OnDefineFilter;        public event OnUpdateOtherEditorHandler OnUpdateOtherEditor;        protected override FrameworkElement CreateEditor()        {            var Grid = new Grid            {                VerticalAlignment = VerticalAlignment.Stretch,                HorizontalAlignment = HorizontalAlignment.Stretch            };            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(30, GridUnitType.Pixel) });            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });            Editor = new TextBox            {                VerticalAlignment = VerticalAlignment.Stretch,                VerticalContentAlignment = VerticalAlignment.Center,                HorizontalAlignment = HorizontalAlignment.Stretch,                IsReadOnly = true            };            Editor.SetValue(Grid.ColumnProperty, 0);            Editor.SetValue(Grid.RowProperty, 0);            Grid.Children.Add(Editor);            var Select = new Button            {                VerticalAlignment = VerticalAlignment.Stretch,                VerticalContentAlignment = VerticalAlignment.Center,                HorizontalAlignment = HorizontalAlignment.Stretch,                Content = "..",                Margin = new Thickness(5, 0, 0, 0),                Focusable = false            };            Select.SetValue(Grid.ColumnProperty, 1);            Select.SetValue(Grid.RowProperty, 0);            Select.Click += Select_Click;            Grid.Children.Add(Select);            var Clear = new Button            {                VerticalAlignment = VerticalAlignment.Stretch,                VerticalContentAlignment = VerticalAlignment.Center,                HorizontalAlignment = HorizontalAlignment.Stretch,                Content = "Clear",                Margin = new Thickness(5, 0, 0, 0),                Focusable = false            };            Clear.SetValue(Grid.ColumnProperty, 2);            Clear.SetValue(Grid.RowProperty, 0);            Clear.Click += Clear_Click;            Grid.Children.Add(Clear);            return Grid;        }        private void Select_Click(object sender, RoutedEventArgs e)        {            if (_type != null)            {                var list = new PopupList(_type, _value, OtherColumns.Keys.ToArray());                list.OnDefineFilter += (o, e) => { return OnDefineFilter?.Invoke(o, e); };                //list.OnDefineFilter += (o, t) => { return OnDefineFilter?.Invoke(o, t); };                if (list.ShowDialog() == true)                {                    _value = list.ID;                    //Dictionary<String, String> fieldmap = new Dictionary<String, String>();                    foreach (var key in OtherColumns.Keys)                        //String[] comps = othercolumn.Split(new String[] { "->" }, StringSplitOptions.None);                        //String field = comps.Last();                        //fieldmap[comps.First()] = field;                        OtherValues[OtherColumns[key]] = list.OtherValues[key];                    //fieldmap[OtherColumns[key]] = list.OtherValues[othercolumn]                    CheckChanged();                    var display = new List<string?>();                    //var columns = Entity.DefaultLookupColumns(_type) as IColumns;                    var columns = LookupFactory.DefineColumns(_type);                    foreach (var column in columns.ColumnNames())                        if (list.OtherValues.ContainsKey(column))                            display.Add(list.OtherValues[column]?.ToString());                    Editor.Text = string.Join(" / ", display.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())));                }            }        }        private void Clear_Click(object sender, RoutedEventArgs e)        {            if (_type != null)            {                _value = Guid.Empty;                foreach (var otherfield in OtherColumns.Keys)                    OtherValues[OtherColumns[otherfield]] = null;                CheckChanged();                Editor.Text = "";            }        }        public override int DesiredHeight()        {            return 25;        }        public override int DesiredWidth()        {            return int.MaxValue;        }        protected override Guid RetrieveValue()        {            return _value;        }        protected override void UpdateValue(Guid value)        {            _value = value;            if (_type is null)            {                Logger.Send(LogType.Error, "", "PopupEditorControl.UpdateValue(): _type is null!");                return;            }            var client = ClientFactory.CreateClient(_type);            var columns = LookupFactory.DefineColumns(_type);            var sort = LookupFactory.DefineSort(_type);            var filter = Filter.Create(_type);            filter.Expression = CoreUtils.GetMemberExpression(_type, "ID");            filter.Operator = Operator.IsEqualTo;            filter.Value = value;            var lookup = client.Query(filter, columns, sort);            var display = new List<object?>();            if (lookup.Rows.Any())                foreach (var col in lookup.Columns.Where(x => !x.ColumnName.Equals("ID")))                    display.Add(lookup.Rows.First()[col.ColumnName]);            Editor.Text = string.Join(" / ", display.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())));        }        public override void Configure()        {            base.Configure();            _type = (EditorDefinition as PopupEditor)?.Type;        }        public override void SetFocus()        {            Editor.Focus();        }        public override void SetColor(Color color)        {            Editor.Background = new SolidColorBrush(color);        }    }}
 |