RecipeManageView.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using ExcelLibrary.BinaryFileFormat;
  2. using CyberX8_MainPages.ViewModels;
  3. using CyberX8_Themes.UserControls;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace CyberX8_MainPages.Views
  19. {
  20. /// <summary>
  21. /// RecipeManageView.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class RecipeManageView : UserControl
  24. {
  25. public RecipeManageView()
  26. {
  27. InitializeComponent();
  28. }
  29. //private bool _isSelected;
  30. //public bool IsSelected
  31. //{
  32. // get { return _isSelected; }
  33. // set
  34. // {
  35. // _isSelected = value;
  36. // }
  37. //}
  38. List<string> FileNameList = new List<string>();
  39. List<string> FilePathList = new List<string>();
  40. List<CheckBox> checkBoxes = new List<CheckBox>();
  41. public bool flag;
  42. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  43. {
  44. (this.DataContext as RecipeManageViewModel).LoadData("");
  45. FileNameList.Clear();
  46. FilePathList.Clear();
  47. }
  48. private void CheckBox_Click(object sender, RoutedEventArgs e)
  49. {
  50. if (sender is CheckBox _checkBox)
  51. {
  52. // 获取当前 CheckBox 的父节点 TreeViewItem
  53. TreeViewItem parentItem = FindVisualParent<TreeViewItem>(_checkBox);
  54. if (_checkBox.Content.ToString() == "Engineering" || _checkBox.Content.ToString() == "Production")
  55. {
  56. bool IsRoot = (bool)_checkBox.DataContext.GetType().GetProperty("IsSelected").GetValue(_checkBox.DataContext);
  57. flag = false;
  58. // 遍历父节点下的所有子节点
  59. foreach (object item in parentItem.Items)
  60. {
  61. TreeViewItem childItem = parentItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
  62. if (childItem != null)
  63. {
  64. // 找到子节点下的 CheckBox
  65. CheckBox childCheckBox = FindVisualChild<CheckBox>(childItem);
  66. if (childCheckBox != null)
  67. {
  68. // 设置子节点的选中状态与父节点一致
  69. object SelectFilePath = _checkBox.DataContext.GetType().GetProperty("RecipeFullFileName").GetValue(childCheckBox.DataContext);
  70. string _selectFilePath = SelectFilePath.ToString();
  71. object SelectFilenName = _checkBox.DataContext.GetType().GetProperty("FileName").GetValue(childCheckBox.DataContext);
  72. string _selectFileName = SelectFilenName.ToString();
  73. childCheckBox.IsChecked = _checkBox.IsChecked;
  74. if (_checkBox.IsChecked == true)
  75. {
  76. //FilePathList.Add(_selectFilePath);
  77. //FileNameList.Add(_selectFileName);
  78. }
  79. else
  80. {
  81. FilePathList.Remove(_selectFilePath );
  82. FileNameList.Remove(_selectFileName);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. else
  89. {
  90. object SelectFilePath = _checkBox.DataContext.GetType().GetProperty("RecipeFullFileName").GetValue(_checkBox.DataContext);
  91. string _selectFilePath = SelectFilePath.ToString();
  92. object SelectFilenName = _checkBox.DataContext.GetType().GetProperty("FileName").GetValue(_checkBox.DataContext);
  93. string _selectFileName = SelectFilenName.ToString();
  94. if (flag==false&&FileNameList.Contains(_selectFileName))
  95. {
  96. (this.DataContext as RecipeManageViewModel).FilePaths = FilePathList;
  97. (this.DataContext as RecipeManageViewModel).FileNames = FileNameList;
  98. return;
  99. }
  100. if (_checkBox.IsChecked == true)
  101. {
  102. FilePathList.Add(_selectFilePath);
  103. FileNameList.Add(_selectFileName);
  104. }
  105. else
  106. {
  107. FilePathList.Remove(_selectFilePath);
  108. FileNameList.Remove(_selectFileName);
  109. }
  110. flag = true;
  111. }
  112. (this.DataContext as RecipeManageViewModel).FilePaths = FilePathList;
  113. (this.DataContext as RecipeManageViewModel).FileNames = FileNameList;
  114. }
  115. }
  116. // 递归设置子节点的 CheckBox
  117. private void SetChildrenCheckBoxes(CheckBox checkBox, bool isChecked)
  118. {
  119. TreeViewItem parentItem = FindVisualParent<TreeViewItem>(checkBox);
  120. if (parentItem != null)
  121. {
  122. foreach (object item in parentItem.Items)
  123. {
  124. TreeViewItem childItem = parentItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
  125. if (childItem != null)
  126. {
  127. CheckBox childCheckBox = FindVisualChild<CheckBox>(childItem);
  128. if (childCheckBox != null)
  129. {
  130. childCheckBox.IsChecked = isChecked;
  131. if (childItem.HasItems)
  132. {
  133. SetChildrenCheckBoxes(childCheckBox, isChecked);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. // 辅助方法,用于在可视树中查找指定类型的父节点或子节点
  141. private T FindVisualParent<T>(DependencyObject obj) where T : DependencyObject
  142. {
  143. while (obj != null)
  144. {
  145. if (obj is T)
  146. {
  147. return (T)obj;
  148. }
  149. obj = VisualTreeHelper.GetParent(obj);
  150. }
  151. return null;
  152. }
  153. private T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
  154. {
  155. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  156. {
  157. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  158. if (child != null && child is T)
  159. {
  160. return (T)child;
  161. }
  162. else
  163. {
  164. T childOfChild = FindVisualChild<T>(child);
  165. if (childOfChild != null)
  166. {
  167. return childOfChild;
  168. }
  169. }
  170. }
  171. return null;
  172. }
  173. private void DeleteSelectedAll(object sender, RoutedEventArgs e)
  174. {
  175. }
  176. private void treeView_Selected(object sender, RoutedEventArgs e)
  177. {
  178. }
  179. }
  180. }