UCStandGrid.xaml.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. using MeterVision.Config;
  2. using MeterVision.db;
  3. using MeterVision.Dlg;
  4. using MeterVision.Helper;
  5. using MeterVision.model;
  6. using MeterVision.Patch;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.ComponentModel;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Data;
  19. using System.Windows.Documents;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Imaging;
  23. using System.Windows.Navigation;
  24. namespace MeterVision.Stand
  25. {
  26. /// <summary>
  27. /// UCStandGrid.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class UCStandGrid : UserControl, INotifyPropertyChanged
  30. {
  31. public event PropertyChangedEventHandler PropertyChanged;
  32. protected void OnPropertyChanged(string propertyName)
  33. {
  34. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  35. }
  36. //public event EventHandler<StandItemCountChangedEventArgs> OnStandItemCountChanged;
  37. //StandItem standItem
  38. //public event Action<StandItem> OnStandItemCountChanged;
  39. private SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  40. private SemaphoreSlim _loadLock = new SemaphoreSlim(1, 1);
  41. //定义数据源
  42. public BulkObservableCollection<StandDetailItem> StandDetailList { get; set; }
  43. //定义事件,用于通知外部组件选中项已更改
  44. public event EventHandler<StandDetailItemChangedEventArgs> OnSelectedStandDetailItemChanged;
  45. public event Action<string> OnDeleteStandDetailItem;
  46. public event Action<string, string> OnStandDetailMark;
  47. private StandDetailItem _selectedStandDetailsItem;
  48. //定义SelectedDataItem属性,并在值变化时触发事件
  49. public StandDetailItem SelectedStandDetailItem
  50. {
  51. get => _selectedStandDetailsItem;
  52. set
  53. {
  54. if(_selectedStandDetailsItem != value)
  55. {
  56. _selectedStandDetailsItem = value;
  57. OnPropertyChanged(nameof(SelectedStandDetailItem));
  58. OnSelectedStandDetailItemChanged?.Invoke(this, new StandDetailItemChangedEventArgs(value));
  59. }
  60. }
  61. }
  62. //private StandItem _curStandItem;
  63. //public StandItem CurStandItem
  64. //{
  65. // get => _curStandItem;
  66. // set
  67. // {
  68. // if (_curStandItem != value)
  69. // {
  70. // _curStandItem = value;
  71. // //StandDetailPage.InitDefaulValue(mConfigItem.StandPageSize);
  72. // //LoadStandDetailItemList(value);
  73. // }
  74. // }
  75. //}
  76. private StationItem _curStationItem;
  77. public StationItem CurStationItem
  78. {
  79. get => _curStationItem;
  80. set
  81. {
  82. /*if(_curStationId != value)
  83. {
  84. _curStationId = value;
  85. }*/
  86. if(_curStationItem != value)
  87. {
  88. //if (!_semaphore.Wait(0)) return; // 防止重复调用
  89. //try
  90. //{
  91. _curStationItem = value;
  92. StandDetailPage.InitDefaulValue(mConfigItem.StandPageSize);
  93. LoadStandDetailItemList();
  94. //}
  95. //catch { }
  96. //finally
  97. //{
  98. // _semaphore.Release();
  99. //}
  100. }
  101. }
  102. }
  103. //public void ChangeStation(StandItem standItem,string stationId)
  104. //{
  105. // bool blChangeStationId = false;
  106. // bool blChangeStandItem = false;
  107. // if(_curStationId != stationId)
  108. // {
  109. // _curStationId = stationId;
  110. // blChangeStationId = true;
  111. // }
  112. // if(_curStandItem != standItem)
  113. // {
  114. // _curStandItem = standItem;
  115. // blChangeStandItem = true;
  116. // }
  117. // if(blChangeStationId || blChangeStandItem)
  118. // {
  119. // StandDetailPage.InitDefaulValue(mConfigItem.StandPageSize);
  120. // LoadStandDetailItemList(CurStandItem);
  121. // }
  122. //}
  123. //详情分页信息
  124. public PageModel StandDetailPage { get; set; }
  125. private int _totalRecord;
  126. public int TotalRecords
  127. {
  128. get => _totalRecord;
  129. set
  130. {
  131. if(_totalRecord != value)
  132. {
  133. _totalRecord = value;
  134. OnPropertyChanged(nameof(TotalRecords));
  135. }
  136. }
  137. }
  138. public CfginiItem mConfigItem { get; set; }
  139. //异步方法
  140. private async void LoadStandDetailItemList()
  141. {
  142. if (!await _loadLock.WaitAsync(0)) return; // 如果已有任务在执行,则直接返回
  143. try
  144. {
  145. StandDetailList.Clear();
  146. if (CurStationItem != null)
  147. {
  148. try
  149. {
  150. var result = await Task.Run(() =>
  151. {
  152. return DBStand.GetPagedStandDetails(
  153. StandDetailPage.PageNumber, StandDetailPage.PageSize, CurStationItem);
  154. });
  155. TotalRecords = result.Item1;
  156. StandDetailPage.PageCount = result.Item2;
  157. List<TStandDetail> standDetails = result.Item3;
  158. //int index = 0;
  159. //foreach (TStandDetail standDetail in standDetails)
  160. //{
  161. // StandDetailList.Add(new StandDetailItem(standDetail));
  162. // index++;
  163. // //更新索引号
  164. // StandDetailList[StandDetailList.Count - 1].Index =
  165. // index + ((StandDetailPage.PageNumber - 1) * StandDetailPage.PageSize);
  166. //}
  167. var standDetailList = standDetails.Select((standDetail, i) => new StandDetailItem(standDetail)
  168. {
  169. Index = (StandDetailPage.PageNumber - 1) * StandDetailPage.PageSize + i + 1
  170. }).ToList();
  171. StandDetailList.AddRange(standDetailList);
  172. if (StandDetailList.Count > 0)
  173. {
  174. //await Task.Delay(100);
  175. //await Dispatcher.InvokeAsync(() =>
  176. //{
  177. //dgStandDetails.ScrollIntoView(dgStandDetails.Items[0]);
  178. //});
  179. var scrollViewer = GetScrollViewer(dgStandDetails);
  180. scrollViewer?.ScrollToTop();
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. MessageBox.Show(Application.Current.MainWindow,
  186. $"加载数据时发生错误:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  187. }
  188. }
  189. if (StandDetailList.Count > 0)
  190. {
  191. SelectedStandDetailItem = StandDetailList[0];
  192. }
  193. else
  194. {
  195. SelectedStandDetailItem = null;
  196. }
  197. }
  198. catch { }
  199. finally
  200. {
  201. _loadLock.Release();
  202. }
  203. }
  204. public UCStandGrid()
  205. {
  206. InitializeComponent();
  207. StandDetailList = new BulkObservableCollection<StandDetailItem>();
  208. dgStandDetails.ItemsSource = StandDetailList;
  209. //CurStandItem = null;
  210. CurStationItem = null;
  211. mConfigItem = CfginiItem.GetConfigItem();
  212. StandDetailPage = new PageModel()
  213. {
  214. PageSize = mConfigItem.PageSize3, //mConfigItem.StandPageSize,
  215. PageNumber = 1,
  216. };
  217. //mConfigItem.OnStandPageSizeChanged += MConfigItem_OnStandPageSizeChanged1;
  218. mConfigItem.OnPageSize3Changed += MConfigItem_OnPageSize3Changed;
  219. UCPatchGrid.OnDeleteStandDetail += UCPatchGrid_OnDeleteStandDetail;
  220. UCPatchGrid.OnUpdateStandValue += UCPatchGrid_OnUpdateStandValue;
  221. this.DataContext = this;
  222. }
  223. private void MConfigItem_OnPageSize3Changed(int pageSize3)
  224. {
  225. //throw new NotImplementedException();
  226. if (!_semaphore.Wait(0)) return; // 防止重复调用
  227. try
  228. {
  229. StandDetailPage.InitDefaulValue(pageSize3);
  230. LoadStandDetailItemList();
  231. }
  232. catch { }
  233. finally
  234. {
  235. _semaphore.Release();
  236. }
  237. }
  238. //private void MConfigItem_OnStandPageSizeChanged1(int pageSize)
  239. //{
  240. // StandDetailPage.InitDefaulValue(pageSize);
  241. // LoadStandDetailItemList();
  242. //}
  243. private void UCPatchGrid_OnUpdateStandValue(string arg1, string arg2)
  244. {
  245. //throw new NotImplementedException();
  246. StandDetailItem standDetailItem = StandDetailList.FirstOrDefault(a => a.StandDetailId == arg1);
  247. if(standDetailItem != null)
  248. {
  249. standDetailItem.StandValue = arg2;
  250. }
  251. }
  252. private void UCPatchGrid_OnDeleteStandDetail(string standDetailId)
  253. {
  254. //Application.Current.Dispatcher.Invoke(() =>
  255. //{
  256. // // 从数据源中移除该条目
  257. // StandDetailList.Remove(selectedItem);
  258. // SelectedStandDetailItem = null;
  259. // OnDeleteStandDetailItem?.Invoke(selectedItem.StandId);
  260. //});
  261. StandDetailItem standDetailItem = StandDetailList.FirstOrDefault(a => a.StandDetailId == standDetailId);
  262. if (standDetailItem != null)
  263. {
  264. StandDetailList.Remove(standDetailItem);
  265. OnDeleteStandDetailItem?.Invoke(standDetailItem.StandId);
  266. }
  267. }
  268. private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  269. {
  270. var image = sender as System.Windows.Controls.Image;
  271. if (image == null) return;
  272. var dataContext = image.DataContext as StandDetailItem;
  273. if (dataContext == null) return;
  274. //var dialog = new ImageViewerWindow(dataContext.SourceImagePath)
  275. var dialog = new imgWindow(dataContext.SrcImage)
  276. {
  277. Owner = Application.Current.MainWindow
  278. };
  279. dialog.Show();
  280. }
  281. private void StandValue_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  282. {
  283. if (e.ClickCount == 2)
  284. {
  285. // 获取当前行的数据上下文
  286. var textBlock = sender as TextBlock;
  287. if (textBlock == null) return;
  288. var dataContext = textBlock.DataContext as StandDetailItem; // 替换为你的数据类型
  289. if (dataContext == null) return;
  290. SelectedStandDetailItem = dataContext;
  291. UpdateStandvalue(dataContext);
  292. }//e.clickCount
  293. }
  294. private void UpdateStandvalue(StandDetailItem detailItem)
  295. {
  296. StandValueModel standValueModel = new StandValueModel(detailItem);
  297. var dialog = new EditStandValueDlg(standValueModel)
  298. {
  299. Owner = Application.Current.MainWindow,
  300. WindowStartupLocation = WindowStartupLocation.CenterOwner
  301. };
  302. //if (dialog.ShowDialog() == true)
  303. //{
  304. // UpdateStandValue(detailItem, standValueModel);
  305. //}//if showDialog
  306. dialog.OnEditStandValue += (value) =>
  307. {
  308. UpdateStandValue(detailItem, standValueModel);
  309. };
  310. dialog.Show();
  311. }
  312. private void UpdateStandValue(StandDetailItem detailItem,StandValueModel standValueModel)
  313. {
  314. try
  315. {
  316. //修改数据库
  317. bool updateStandValue2 = DBStand.UpdateStandDetailStandValue(detailItem.SrcImage, standValueModel.StandValue);
  318. if (updateStandValue2)
  319. {
  320. detailItem.StandValue = standValueModel.StandValue;
  321. //MessageBox.Show(Application.Current.MainWindow, "修改标准值完成。", "提示", MessageBoxButton.OK,
  322. // MessageBoxImage.Information);
  323. }
  324. else
  325. {
  326. MessageBox.Show(Application.Current.MainWindow, "修改标准值失败。", "警告", MessageBoxButton.OK,
  327. MessageBoxImage.Warning);
  328. }
  329. }
  330. catch (Exception ex)
  331. {
  332. MessageBox.Show(Application.Current.MainWindow, $"修改标准值错误:{ex.Message}!", "警告", MessageBoxButton.OK,
  333. MessageBoxImage.Warning);
  334. }
  335. }
  336. private void UserControl_DragEnter(object sender, DragEventArgs e)
  337. {
  338. //e.Handled = true;
  339. //// Check if the dragged item is a folder
  340. //if (e.Data.GetDataPresent(DataFormats.FileDrop) && CurStandItem != null)
  341. //{
  342. // var paths = (string[])e.Data.GetData(DataFormats.FileDrop);
  343. // if (paths != null && paths.Length > 0 && Directory.Exists(paths[0]))
  344. // {
  345. // e.Effects = DragDropEffects.Copy;
  346. // }
  347. // else
  348. // {
  349. // e.Effects = DragDropEffects.None;
  350. // }
  351. //}
  352. //else
  353. //{
  354. // e.Effects = DragDropEffects.None;
  355. //}
  356. }
  357. //private async void UserControl_Drop(object sender, DragEventArgs e)
  358. //{
  359. //if (e.Data.GetDataPresent(DataFormats.FileDrop) && CurStandItem != null)
  360. //{
  361. // var paths = (string[])e.Data.GetData(DataFormats.FileDrop);
  362. // if (paths != null && paths.Length > 0)
  363. // {
  364. // var folderPath = paths[0];
  365. // if (Directory.Exists(folderPath))
  366. // {
  367. // await ImportStandImageFloder(folderPath);
  368. // }
  369. // }
  370. //}//if
  371. //}
  372. //public async Task<bool> ImportStandImageFloder(string folderPath)
  373. //{
  374. // try
  375. // {
  376. // bool blLoad = await LoadJpgFilesFromFolder(folderPath);
  377. // if (blLoad)
  378. // {
  379. // StandDetailPage.InitDefaulValue();
  380. // LoadStandDetailItemList(CurStandItem);
  381. // }
  382. // return true;
  383. // }
  384. // catch(Exception ex)
  385. // {
  386. // MessageBox.Show(Application.Current.MainWindow,
  387. // $"导入模板图像错误:{ex.Message}", "错误",
  388. // MessageBoxButton.OK, MessageBoxImage.Error);
  389. // return false;
  390. // }
  391. //}
  392. //private async Task<bool> LoadJpgFilesFromFolder(string folderPath)
  393. //{
  394. // //var jpgFiles = Directory.GetFiles(folderPath, "*.jpg", SearchOption.AllDirectories);
  395. // var jpgFiles = Directory.GetFiles(folderPath, "*.jpg", SearchOption.AllDirectories)
  396. // .Concat(Directory.GetFiles(folderPath, "*.bmp", SearchOption.AllDirectories))
  397. // .ToArray();
  398. // List<string> fileList = new List<string>();
  399. // foreach (var filePath in jpgFiles)
  400. // {
  401. // /*_fileItems.Add(new FileItem
  402. // {
  403. // FileName = Path.GetFileName(filePath),
  404. // FilePath = filePath,
  405. // FileSize = new FileInfo(filePath).Length
  406. // });*/
  407. // if( (ThisApp.IsJpgFile(filePath) || ThisApp.IsBmpFile(filePath) ) &&
  408. // ThisApp.IsFileSizeValid(filePath) &&
  409. // ThisApp.IsImageDimensionsValid(filePath))
  410. // {
  411. // fileList.Add(filePath);
  412. // }
  413. // }//foreach
  414. // if(fileList.Count == 0)
  415. // {
  416. // MessageBox.Show(Application.Current.MainWindow,
  417. // "没有找到符号要求的图片文件。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  418. // return false;
  419. // }
  420. // //加载数据到数据库中
  421. // return await InsertFilesWithProgress(fileList);
  422. //}
  423. //public async Task<bool> InsertFilesWithProgress(List<string> filePaths)
  424. //{
  425. // // 弹出一个非模式对话框来显示进度
  426. // ProgressDialog progressDialog = new ProgressDialog()
  427. // {
  428. // Owner = Application.Current.MainWindow,
  429. // WindowStartupLocation = WindowStartupLocation.CenterOwner
  430. // };
  431. // progressDialog.Show();
  432. // try
  433. // {
  434. // // 任务来异步执行文件插入
  435. // bool blInsert = await InsertFilesToDatabase(filePaths, progressDialog);
  436. // //Task.Run(() => InsertFilesToDatabase(filePaths, progressDialog));
  437. // //刷新CurItem
  438. // if (blInsert)
  439. // {
  440. // OnStandItemCountChanged?.Invoke(this, new StandItemCountChangedEventArgs(CurStandItem));
  441. // }
  442. // return blInsert;
  443. // }
  444. // catch (Exception ex)
  445. // {
  446. // MessageBox.Show(Application.Current.MainWindow, $"操作数据库失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  447. // return false;
  448. // }
  449. // finally
  450. // {
  451. // // 关闭进度对话框
  452. // progressDialog.Close();
  453. // }
  454. //}
  455. //public async Task<bool> InsertFilesToDatabase(List<string> filePaths, ProgressDialog progressDialog)
  456. //{
  457. // int totalFiles = filePaths.Count;
  458. // int currentFile = 0;
  459. // int insertCount = 0;
  460. // try
  461. // {
  462. // await Task.Run(() =>
  463. // {
  464. // foreach (var filePath in filePaths)
  465. // {
  466. // // 检查文件是否已经存在于数据库
  467. // if (!DBStand.IsSrcImageExitInStand(filePath, CurStandItem.StandId))
  468. // {
  469. // // 插入文件路径到数据库
  470. // //InsertFileToDatabase(filePath);
  471. // TStandDetail standDetail = new TStandDetail()
  472. // {
  473. // StandDetailId = Guid.NewGuid().ToString(),
  474. // CreateTime = ThisApp.GetNowTime_yyyyMMddHHmmss(),
  475. // StandId = CurStandItem.StandId,
  476. // SrcImage = filePath,
  477. // StandValue = ""
  478. // };
  479. // bool blInsert = DBStand.InsertStandDetail(standDetail);
  480. // if(blInsert)
  481. // {
  482. // insertCount++;
  483. // }
  484. // }
  485. // // 更新进度条
  486. // currentFile++;
  487. // Application.Current.Dispatcher.Invoke(() =>
  488. // {
  489. // UpdateProgress(progressDialog, currentFile, totalFiles);
  490. // });
  491. // }
  492. // });
  493. // }
  494. // catch(Exception ex)
  495. // {
  496. // MessageBox.Show(Application.Current.MainWindow, $"插入数据失败:{ex.Message}。", "错误",
  497. // MessageBoxButton.OK, MessageBoxImage.Error);
  498. // }
  499. // return insertCount > 0;
  500. //}
  501. public void UpdateProgress(ProgressDialog progressDialog, int currentFile, int totalFiles)
  502. {
  503. // 更新进度条的进度
  504. double progress = (double)currentFile / totalFiles * 100;
  505. progressDialog.UpdateProgress(progress);
  506. }
  507. public void NextPage()
  508. {
  509. if (!_semaphore.Wait(0)) return; // 防止重复调用
  510. try
  511. {
  512. if (StandDetailPage.PageNumber < StandDetailPage.PageCount)
  513. {
  514. StandDetailPage.PageNumber += 1;
  515. LoadStandDetailItemList();
  516. }
  517. }
  518. catch { }
  519. finally
  520. {
  521. _semaphore.Release();
  522. }
  523. }
  524. public void PrePage()
  525. {
  526. if (!_semaphore.Wait(0)) return; // 防止重复调用
  527. try
  528. {
  529. if (StandDetailPage.PageNumber > 1)
  530. {
  531. StandDetailPage.PageNumber -= 1;
  532. LoadStandDetailItemList();
  533. }
  534. }
  535. catch { }
  536. finally
  537. {
  538. _semaphore.Release();
  539. }
  540. }
  541. public void FirstPage()
  542. {
  543. if (!_semaphore.Wait(0)) return; // 防止重复调用
  544. try
  545. {
  546. if (StandDetailPage.PageNumber > 1)
  547. {
  548. StandDetailPage.PageNumber = 1;
  549. LoadStandDetailItemList();
  550. }
  551. }
  552. catch { }
  553. finally
  554. {
  555. _semaphore.Release();
  556. }
  557. }
  558. public void LastPage()
  559. {
  560. if (!_semaphore.Wait(0)) return; // 防止重复调用
  561. try
  562. {
  563. if (StandDetailPage.PageNumber < StandDetailPage.PageCount)
  564. {
  565. StandDetailPage.PageNumber = StandDetailPage.PageCount;
  566. LoadStandDetailItemList();
  567. }
  568. }
  569. catch { }
  570. finally
  571. {
  572. _semaphore.Release();
  573. }
  574. }
  575. public void SpeciPage(int pageNumber)
  576. {
  577. if (!_semaphore.Wait(0)) return; // 防止重复调用
  578. try
  579. {
  580. if (pageNumber != StandDetailPage.PageNumber &&
  581. pageNumber > 0 && pageNumber <= StandDetailPage.PageCount)
  582. {
  583. StandDetailPage.PageNumber = pageNumber;
  584. LoadStandDetailItemList();
  585. }
  586. }
  587. catch { }
  588. finally
  589. {
  590. _semaphore.Release();
  591. }
  592. }
  593. private void BtnDelDetailItem_Click(object sender, RoutedEventArgs e)
  594. {
  595. Button button = sender as Button;
  596. if (button == null) return;
  597. DataGridRow row = FindAncestor<DataGridRow>(button);
  598. if (row == null) return;
  599. //获取当前行绑定的数据项
  600. var selectedItem = row.Item as StandDetailItem;
  601. DeleteSelectedItem(selectedItem);
  602. }
  603. private async void DeleteSelectedItem(StandDetailItem selectedItem)
  604. {
  605. if (selectedItem == null) return;
  606. MessageBoxResult result = MessageBox.Show("确定要删除此条目吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
  607. if (result != MessageBoxResult.Yes) return;
  608. string titleInfo = "正在删除,请稍后...";
  609. WaitWindow waitWindow = new WaitWindow(titleInfo)
  610. {
  611. Owner = Application.Current.MainWindow,
  612. WindowStartupLocation = WindowStartupLocation.CenterOwner
  613. };
  614. waitWindow.Show();
  615. try
  616. {
  617. bool deleteSuccess = false;
  618. await Task.Run(() =>
  619. {
  620. deleteSuccess = DBStand.DeleteTStandDetailById(selectedItem.StandDetailId);
  621. });
  622. if (deleteSuccess)
  623. {
  624. //LoadStandDetailItemList(CurStandItem);
  625. //await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  626. //{
  627. // 从数据源中移除该条目
  628. StandDetailList.Remove(selectedItem);
  629. SelectedStandDetailItem = null;
  630. OnDeleteStandDetailItem?.Invoke(selectedItem.StandId);
  631. //重新加载数据
  632. //LoadPatchDetailItemList();
  633. //}));
  634. }
  635. }
  636. catch (Exception ex)
  637. {
  638. MessageBox.Show(Application.Current.MainWindow, $"删除失败:{ex.Message}", "错误",
  639. MessageBoxButton.OK, MessageBoxImage.Error);
  640. }
  641. finally
  642. {
  643. waitWindow.Close();
  644. }
  645. }
  646. public static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
  647. {
  648. var parent = VisualTreeHelper.GetParent(dependencyObject);
  649. if (parent == null) return null;
  650. var parentT = parent as T;
  651. return parentT ?? FindAncestor<T>(parent);
  652. }
  653. private void BtnUpdateStandvalue_Click(object sender, RoutedEventArgs e)
  654. {
  655. Button button = sender as Button;
  656. if (button == null) return;
  657. DataGridRow row = FindAncestor<DataGridRow>(button);
  658. if (row == null) return;
  659. //获取当前行绑定的数据项
  660. var selectedItem = row.Item as StandDetailItem;
  661. UpdateStandvalue(selectedItem);
  662. }
  663. private void MiDelteRow_Click(object sender, RoutedEventArgs e)
  664. {
  665. if(SelectedStandDetailItem != null)
  666. {
  667. DeleteSelectedItem(SelectedStandDetailItem);
  668. }
  669. }
  670. private void MiUpdateStandvalue_Click(object sender, RoutedEventArgs e)
  671. {
  672. if (SelectedStandDetailItem == null) return;
  673. UpdateStandvalue(SelectedStandDetailItem);
  674. }
  675. private void BtnConfig_Click(object sender, RoutedEventArgs e)
  676. {
  677. Button button = sender as Button;
  678. if (button == null) return;
  679. DataGridRow row = FindAncestor<DataGridRow>(button);
  680. if (row == null) return;
  681. //获取当前行绑定的数据项
  682. var selectedItem = row.Item as StandDetailItem;
  683. EditStandDetailCfg dialog = new EditStandDetailCfg(selectedItem)
  684. {
  685. Owner = Application.Current.MainWindow,
  686. WindowStartupLocation = WindowStartupLocation.CenterOwner
  687. };
  688. if (dialog.ShowDialog() == true)
  689. {
  690. selectedItem = dialog.mStandDetail;
  691. OnStandDetailMark?.Invoke(selectedItem.StandId, selectedItem.StandId);
  692. }
  693. }
  694. private ScrollViewer _scrollViewer;
  695. private ScrollViewer GetScrollViewer(DependencyObject obj)
  696. {
  697. if (_scrollViewer != null)
  698. {
  699. return _scrollViewer;
  700. }
  701. if (obj is ScrollViewer)
  702. {
  703. _scrollViewer = (ScrollViewer)obj;
  704. return obj as ScrollViewer;
  705. }
  706. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  707. {
  708. var child = VisualTreeHelper.GetChild(obj, i);
  709. var scrollViewer = GetScrollViewer(child);
  710. if (scrollViewer != null)
  711. {
  712. _scrollViewer = scrollViewer;
  713. return scrollViewer;
  714. }
  715. }
  716. return null;
  717. }
  718. ///////////////////////////////////////////////////////////////////////////
  719. }
  720. public class StandDetailItemChangedEventArgs : EventArgs
  721. {
  722. public StandDetailItem SelectedDataItem { get; }
  723. public StandDetailItemChangedEventArgs(StandDetailItem selectedDataItem)
  724. {
  725. SelectedDataItem = selectedDataItem;
  726. }
  727. }
  728. //目录的条目数发生了变化
  729. //public class StandItemCountChangedEventArgs : EventArgs
  730. //{
  731. // public StandItem mStandItem { get; }
  732. // public StandItemCountChangedEventArgs(StandItem standItem)
  733. // {
  734. // mStandItem = standItem;
  735. // }
  736. //}
  737. ///////////////////////////////////////////////////////////////////
  738. }