123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- using AIPlatform.Protocol.Entities;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- namespace aipdev
- {
- /// <summary>
- /// Interaction logic for CreateImageCaseWindow.xaml
- /// </summary>
- public partial class CreateImageCaseWindow : Window
- {
- /// <summary>
- /// Gets the organization.
- /// </summary>
- public Organization Organization { get; private set; }
- /// <summary>
- /// Gets the image category.
- /// </summary>
- public ImageCategory ImageCategory { get; private set; }
- /// <summary>
- /// Gets the image batch.
- /// </summary>
- public ImageCategory ImageBatch { get; private set; }
- /// <summary>
- /// Gets the image case name.
- /// </summary>
- public string ImageCaseName { get; private set; }
- /// <summary>
- /// Gets if dialog is filled and OK.
- /// </summary>
- public bool Ok { get; private set; }
- /// <summary>
- /// Gets the selected organization id.
- /// </summary>
- private long SelectedOrganizationId { get; set; }
- /// <summary>
- /// Gets the selected image category id.
- /// </summary>
- private long SelectedImageCategoryId { get; set; }
- /// <summary>
- /// Gets the selected image batch id.
- /// </summary>
- private long SelectedImageBatchId { get; set; }
- private List<ImageCategory> _lstImageCategories = new List<ImageCategory>();
- private List<ImageCategory> _lstImageBatches = new List<ImageCategory>();
- private string _imageCategory;
- private string _imageBatch;
- private EntityBase _developer;
- public CreateImageCaseWindow(long organizationId, long categoryId, long batchId, EntityBase developer)
- {
- InitializeComponent();
- SelectedOrganizationId = organizationId;
- SelectedImageCategoryId = categoryId;
- SelectedImageBatchId = batchId;
- _developer = developer;
- Loaded += OnLoaded;
- txtImageCase.Focus();
- txtImageCase.Text = "老数据导入_";
- txtImageCase.SelectionStart = txtImageCase.Text.Length;
- }
- private async void OnLoaded(object sender, RoutedEventArgs e)
- {
- try
- {
- cbbOrganizations.SelectionChanged -= OnOrganizationSelectionChanged;
- cbbImageCategories.SelectionChanged -= cbbImageCategories_SelectionChanged;
- cbbImageBatches.SelectionChanged -= OnImageBatchSelectionChanged;
- cbbOrganizations.Items.Clear();
- var organizations = await DeveloperManager.Shared.GetOrganizationsAsync();
- if (organizations != null)
- {
- foreach (var item in organizations)
- {
- cbbOrganizations.Items.Add(item);
- }
- if (organizations.Count > 0)
- {
- cbbOrganizations.SelectedIndex = organizations.FindIndex(x => x.Id == SelectedOrganizationId);
- //ImageCategories
- var categories = await DeveloperManager.Shared.GetImageCategoriesByDeveloperAsync(SelectedOrganizationId, _developer);
- if (categories != null)
- {
- cbbImageCategories.Items.Clear();
- foreach (var item in categories)
- {
- cbbImageCategories.Items.Add(item);
- }
- if (categories.Count > 0)
- {
- cbbImageCategories.SelectedIndex = categories.FindIndex(x => x.Id == SelectedImageCategoryId);
- //ImageBtaches
- var imageBtaches = await DeveloperManager.Shared.GetImageCategoriesAsync(SelectedImageCategoryId);
- if (imageBtaches != null)
- {
- cbbImageBatches.Items.Clear();
- foreach (var subItem in imageBtaches)
- {
- cbbImageBatches.Items.Add(subItem);
- }
- if (imageBtaches.Count > 0)
- {
- cbbImageBatches.SelectedIndex = imageBtaches.FindIndex(x => x.Id == SelectedImageBatchId);
- }
- _lstImageBatches = imageBtaches;
- }
- }
- _lstImageCategories = categories;
- }
- }
- }
- _imageCategory = cbbImageCategories.Text;
- _imageBatch = cbbImageBatches.Text;
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"加载数据失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- finally
- {
- cbbOrganizations.SelectionChanged += OnOrganizationSelectionChanged;
- cbbImageCategories.SelectionChanged += cbbImageCategories_SelectionChanged;
- cbbImageBatches.SelectionChanged += OnImageBatchSelectionChanged;
- }
- }
- private void OnOkClick(object sender, RoutedEventArgs e)
- {
- if (cbbOrganizations.SelectedIndex == -1)
- {
- MessageBox.Show(Application.Current.MainWindow, "请选择组织", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- if (cbbImageCategories.SelectedIndex == -1)
- {
- MessageBox.Show(Application.Current.MainWindow, "请选择图像大类", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- if (cbbImageBatches.SelectedIndex == -1)
- {
- MessageBox.Show(Application.Current.MainWindow, "请选择图像批次", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- if (string.IsNullOrWhiteSpace(txtImageCase.Text) || string.IsNullOrEmpty(txtImageCase.Text))
- {
- MessageBox.Show(Application.Current.MainWindow, "请填写用例名称", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- Organization = cbbOrganizations.SelectedItem as Organization;
- ImageCategory = cbbImageCategories.SelectedItem as ImageCategory;
- ImageBatch = cbbImageBatches.SelectedItem as ImageCategory;
- ImageCaseName = txtImageCase.Text.Trim();
- Ok = true;
- Close();
- }
- private void OnCloseClick(object sender, RoutedEventArgs e)
- {
- Ok = false;
- Close();
- }
- private async void OnOrganizationSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
- {
- try
- {
- cbbImageCategories.Items.Clear();
- var parent = cbbOrganizations.SelectedItem as Organization;
- if (parent != null)
- {
- await OnSelectionChanged(parent.Id, cbbImageCategories);
- _lstImageCategories = new List<ImageCategory>();
- foreach (var item in cbbImageCategories.Items)
- {
- var newitem = (ImageCategory)item;
- _lstImageCategories.Add(newitem);
- }
- }
- _imageCategory = cbbImageCategories.Text;
- _imageBatch = cbbImageBatches.Text;
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"选择组织失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- private async void cbbImageCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- cbbImageBatches.Items.Clear();
- var parent = cbbImageCategories.SelectedItem as ImageCategory;
- if (parent != null)
- {
- await OnSelectionChanged(parent.Id, cbbImageBatches);
- _lstImageBatches = new List<ImageCategory>();
- foreach (var item in cbbImageBatches.Items)
- {
- var newitem = (ImageCategory)item;
- _lstImageBatches.Add(newitem);
- }
- }
- if (this.cbbImageCategories.SelectedValue != null)
- {
- _imageCategory = this.cbbImageCategories.SelectedValue.ToString();
- }
- }
- private void OnImageBatchSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (cbbImageBatches.SelectedValue != null)
- {
- _imageBatch = cbbImageBatches.SelectedValue.ToString();
- }
- }
- private async Task OnSelectionChanged(long parentId, ComboBox targetCbb)
- {
- try
- {
- var categories = new List<ImageCategory>();
- if (targetCbb.Name == "cbbImageCategories")
- {
- categories = await DeveloperManager.Shared.GetImageCategoriesByDeveloperAsync(parentId, _developer);
- }
- else
- {
- categories = await DeveloperManager.Shared.GetImageCategoriesAsync(parentId);
- }
- if (categories != null)
- {
- targetCbb.Items.Clear();
- foreach (var item in categories)
- {
- targetCbb.Items.Add(item);
- }
- if (categories.Count > 0)
- {
- targetCbb.SelectedIndex = 0;
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"选择失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- #region 模糊查询方法提供
- private void OnImageCategoryKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
- {
- if (_imageCategory != this.cbbImageCategories.Text)
- {
- this.OnInputFilter(this.cbbImageCategories, _lstImageCategories);
- _imageCategory = this.cbbImageCategories.Text;
- }
- }
- private void OnImageBatchKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
- {
- }
- private void OnInputFilter(ComboBox targetCbb, List<ImageCategory> list)
- {
- try
- {
- TextBox textBox = (TextBox)targetCbb.Template.FindName("PART_EditableTextBox", targetCbb);
- int startIndex = textBox.SelectionStart;
- string searchText = targetCbb.Text.Trim();
- targetCbb.Items.Clear();
- targetCbb.Text = searchText;
- List<ImageCategory> newlist = list;
- if (!string.IsNullOrEmpty(searchText) && list.Count > 0)
- {
- newlist = list.Where(x => x.Name.ToLower().Contains(searchText.ToLower())).ToList();
- }
- foreach (var item in newlist)
- {
- targetCbb.Items.Add(item);
- }
- targetCbb.IsDropDownOpen = true;
- textBox.SelectionStart = startIndex;
- textBox.Select(startIndex, 0);
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"选择失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- #endregion
- }
- }
|