public override void CustomTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.F2 && OpenWindowCommand.CanExecute(null)) { OpenWindowCommand.Execute(null); } else if (e.Key == Key.Enter) { if (string.IsNullOrEmpty(ValueColumn) || string.IsNullOrEmpty(IdColumn) || string.IsNullOrEmpty((sender as CustomTextBox).Text) || (SearchCommand == null && ItemsSource == null)) { ClearLookup(); return; } if (PreviewSelect != null) { var arg = new PreviewSelectEventArgs(); PreviewSelect(this, arg); if (arg.Cancel == true) { ClearLookup(); return; } } LoadData(); if (valueColumnPropInfo == null || idColumnPropInfo == null) { ClearLookup(); return; } if (SelectionMode == LookupSelectionMode.Single) { IEnumerable result = null; if (SearchCommand == null) { try { result = ItemsSource.Where($@"{CodeColumn}=={(sender as CustomTextBox).Text}", null); } catch { ClearLookup(); return; } } else { try { SearchCommand?.Execute(new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>(CodeColumn, $@"{CodeColumn}=={(sender as CustomTextBox).Text}") }); result = ItemsSource; } catch { ClearLookup(); return; } } if (result != null && result.Count() > 0) { ChangeSource = ChangeSourceEnum.FromComponent; foreach (var item in result) { SelectedItem = item; SelectedIndex = GetIndex(item); SelectedId = idColumnPropInfo?.GetValue(item) as int?; break; } ChangeSource = ChangeSourceEnum.None; } else { ClearLookup(); } } else { var strCode = (sender as CustomTextBox).Text.Split(','); for (int i = 0; i < strCode.Length; i++) { strCode[i] = strCode[i].Trim(); } strCode = strCode.Distinct().ToArray(); int counter = 0; var itemList = new List <object>(); var indexList = new List <int>(); var idList = new List <int>(); if (ItemsSource != null) { foreach (var item in ItemsSource) { var code = codeColumnPropInfo?.GetValue(item)?.ToString(); if (!string.IsNullOrEmpty(code)) { if (strCode.Any(x => x == code)) { itemList.Add(item); indexList.Add(counter); var id = idColumnPropInfo?.GetValue(item) as int?; } } counter++; } } if (itemList.Any()) { ChangeSource = ChangeSourceEnum.FromComponent; SelectedItems = itemList; SelectedIndices = indexList; SelectedIds = idList; ChangeSource = ChangeSourceEnum.None; } else { ClearLookup(); } } } }
private void OpenWindowExcecute() { if (PreviewSelect != null) { var arg = new PreviewSelectEventArgs(); PreviewSelect(this, arg); if (arg.Cancel == true) { ClearLookup(); return; } } LoadData(); if (valueColumnPropInfo == null || idColumnPropInfo == null) { ClearLookup(); return; } PrepareLookupWindow(); if (SelectionMode == LookupSelectionMode.Single) { lookupWindow.ShowDialog(); } else { if (ItemsSource == null || ItemsSource.Count() == 0) { ClearLookup(); return; } else { foreach (var item in ItemsSource) { if (!(item is ISelectable)) { return; } break; } } if (SelectedIndices != null) { foreach (var item in SelectedItems) { (item as ISelectable).IsSelected = true; } } if (lookupWindow.ShowDialog() == true) { var itemList = new List <object>(); var indexList = new List <int>(); var idList = new List <int>(); int counter = -1; foreach (var item in ItemsSource) { counter++; if ((item as ISelectable).IsSelected) { indexList.Add(counter); itemList.Add(item); var id = idColumnPropInfo?.GetValue(item) as int?; if (id != null) { idList.Add(id.Value); } } } if (itemList.Count > 0) { ChangeSource = ChangeSourceEnum.FromComponent; SelectedItems = itemList; SelectedIndices = indexList; SelectedIds = idList; ChangeSource = ChangeSourceEnum.None; } else { ClearLookup(); } } } }
private void OpenWindowExcecute() { if (PreviewSelect != null) { var arg = new PreviewSelectEventArgs(); PreviewSelect(this, arg); if (arg.Cancel == true) { ClearLookup(); return; } } LoadData(); if (valueColumnPropInfo == null || idColumnPropInfo == null) { ClearLookup(); return; } PrepareLookupWindow(); if (SelectionMode == LookupSelectionMode.Single) { treeLookupWindow.ShowDialog(); } else { if (ItemsSource == null) { ClearLookup(); return; } if (SelectedItems != null) { foreach (var item in SelectedItems) { if (item is ISelectable) { (item as ISelectable).IsSelected = true; } } } if (treeLookupWindow.ShowDialog() == true) { if (typeof(ISelectable).IsAssignableFrom(ResultType)) { var itemList = new List <object>(); var idList = new List <int>(); var list = FlatItemsSource.Cast <ISelectable>().Where(x => x.IsSelected); foreach (var item in list) { itemList.Add(item); var id = idColumnPropInfo?.GetValue(item) as int?; if (id != null) { idList.Add(id.Value); } } if (itemList.Count > 0) { ChangeSource = ChangeSourceEnum.FromComponent; SelectedItems = itemList; SelectedIds = idList; ChangeSource = ChangeSourceEnum.None; } else { ClearLookup(); } } } } }