123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using ExcelLibrary.BinaryFileFormat;
- using CyberX8_MainPages.ViewModels;
- using CyberX8_Themes.UserControls;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace CyberX8_MainPages.Views
- {
- /// <summary>
- /// RecipeManageView.xaml 的交互逻辑
- /// </summary>
- public partial class RecipeManageView : UserControl
- {
- public RecipeManageView()
- {
- InitializeComponent();
- }
- //private bool _isSelected;
- //public bool IsSelected
- //{
- // get { return _isSelected; }
- // set
- // {
- // _isSelected = value;
- // }
- //}
- List<string> FileNameList = new List<string>();
- List<string> FilePathList = new List<string>();
- List<CheckBox> checkBoxes = new List<CheckBox>();
- public bool flag;
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- (this.DataContext as RecipeManageViewModel).LoadData("");
- FileNameList.Clear();
- FilePathList.Clear();
- }
-
- private void CheckBox_Click(object sender, RoutedEventArgs e)
- {
- if (sender is CheckBox _checkBox)
- {
- // 获取当前 CheckBox 的父节点 TreeViewItem
- TreeViewItem parentItem = FindVisualParent<TreeViewItem>(_checkBox);
- if (_checkBox.Content.ToString() == "Engineering" || _checkBox.Content.ToString() == "Production")
- {
- bool IsRoot = (bool)_checkBox.DataContext.GetType().GetProperty("IsSelected").GetValue(_checkBox.DataContext);
- flag = false;
- // 遍历父节点下的所有子节点
- foreach (object item in parentItem.Items)
- {
- TreeViewItem childItem = parentItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
- if (childItem != null)
- {
- // 找到子节点下的 CheckBox
- CheckBox childCheckBox = FindVisualChild<CheckBox>(childItem);
- if (childCheckBox != null)
- {
- // 设置子节点的选中状态与父节点一致
- object SelectFilePath = _checkBox.DataContext.GetType().GetProperty("RecipeFullFileName").GetValue(childCheckBox.DataContext);
- string _selectFilePath = SelectFilePath.ToString();
- object SelectFilenName = _checkBox.DataContext.GetType().GetProperty("FileName").GetValue(childCheckBox.DataContext);
- string _selectFileName = SelectFilenName.ToString();
- childCheckBox.IsChecked = _checkBox.IsChecked;
- if (_checkBox.IsChecked == true)
- {
- //FilePathList.Add(_selectFilePath);
- //FileNameList.Add(_selectFileName);
- }
- else
- {
- FilePathList.Remove(_selectFilePath );
- FileNameList.Remove(_selectFileName);
- }
- }
- }
- }
- }
- else
- {
- object SelectFilePath = _checkBox.DataContext.GetType().GetProperty("RecipeFullFileName").GetValue(_checkBox.DataContext);
- string _selectFilePath = SelectFilePath.ToString();
- object SelectFilenName = _checkBox.DataContext.GetType().GetProperty("FileName").GetValue(_checkBox.DataContext);
- string _selectFileName = SelectFilenName.ToString();
- if (flag==false&&FileNameList.Contains(_selectFileName))
- {
- (this.DataContext as RecipeManageViewModel).FilePaths = FilePathList;
- (this.DataContext as RecipeManageViewModel).FileNames = FileNameList;
- return;
- }
- if (_checkBox.IsChecked == true)
- {
- FilePathList.Add(_selectFilePath);
- FileNameList.Add(_selectFileName);
- }
- else
- {
- FilePathList.Remove(_selectFilePath);
- FileNameList.Remove(_selectFileName);
- }
- flag = true;
- }
- (this.DataContext as RecipeManageViewModel).FilePaths = FilePathList;
- (this.DataContext as RecipeManageViewModel).FileNames = FileNameList;
- }
- }
- // 递归设置子节点的 CheckBox
- private void SetChildrenCheckBoxes(CheckBox checkBox, bool isChecked)
- {
- TreeViewItem parentItem = FindVisualParent<TreeViewItem>(checkBox);
- if (parentItem != null)
- {
- foreach (object item in parentItem.Items)
- {
- TreeViewItem childItem = parentItem.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
- if (childItem != null)
- {
- CheckBox childCheckBox = FindVisualChild<CheckBox>(childItem);
- if (childCheckBox != null)
- {
- childCheckBox.IsChecked = isChecked;
- if (childItem.HasItems)
- {
- SetChildrenCheckBoxes(childCheckBox, isChecked);
- }
- }
- }
- }
- }
- }
- // 辅助方法,用于在可视树中查找指定类型的父节点或子节点
- private T FindVisualParent<T>(DependencyObject obj) where T : DependencyObject
- {
- while (obj != null)
- {
- if (obj is T)
- {
- return (T)obj;
- }
- obj = VisualTreeHelper.GetParent(obj);
- }
- return null;
- }
- private T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild(obj, i);
- if (child != null && child is T)
- {
- return (T)child;
- }
- else
- {
- T childOfChild = FindVisualChild<T>(child);
- if (childOfChild != null)
- {
- return childOfChild;
- }
- }
- }
- return null;
- }
- private void DeleteSelectedAll(object sender, RoutedEventArgs e)
- {
- }
- private void treeView_Selected(object sender, RoutedEventArgs e)
- {
- }
- }
- }
|