using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Media; using XCommon.Log; using XCore; using XHandler.Class.DataEx; using XImagingXhandler.XDAL; namespace XHandler.View.MethodProperty { /// /// DataExportProperty.xaml 的交互逻辑 /// public partial class DataExportProperty : System.Windows.Controls.UserControl { public MethodDataExport methodDataExport { get; set; } public MethodDataExport currmethodDataExport = null; List dropdownNames = new List(); #region 变量 string isrun = ""; string status = ""; string name = ""; string label = ""; string filePath = ""; List reportColumns = new List(); string filePostfix = ""; string filename = ""; int mark = 0; #endregion public DataExportProperty() { InitializeComponent(); } public DataExportProperty(MethodEx method) { InitializeComponent(); mark = 0; DataExportBll dataExportBll = new DataExportBll(); dropdownNames = dataExportBll.GenerateReportColumn(); //dgTransferFileData.ItemsSource= dropdownNames; BindReportColumn(); methodDataExport = new MethodDataExport(); methodDataExport.name = method.method_name; methodDataExport.label = method.method_name; methodDataExport.status = (method.isEnabled == true ? "enable" : "disable"); methodDataExport.strIndex = method.strIndex; methodDataExport.reportColumns = new List(); foreach (DropdownName d in dropdownNames) { ReportColumn r = new ReportColumn(); r.reportcolumn_id = d.dropdown_id; r.reportcolumn_name = d.dropdown_name; r.reportcolumn_sname = d.dropdown_name; methodDataExport.reportColumns.Add(r); } DataContext = methodDataExport; if (method.tag != null) { methodDataExport = (MethodDataExport)method.tag; currmethodDataExport = methodDataExport; isrun = methodDataExport.isrun; status = methodDataExport.status; name = methodDataExport.name; label = methodDataExport.label; filePath = methodDataExport.filePath; reportColumns = methodDataExport.reportColumns; filePostfix = methodDataExport.filePostfix; filename = methodDataExport.fileName; } } private void UserControl_Loaded(object sender, RoutedEventArgs e) { mark += 1; try { if (mark > 1) { return; } if (!string.IsNullOrEmpty(filePostfix)) { //foreach (DropdownName item in dgTransferFileData.Items) //{ // DataGridTemplateColumn templeColumn = dgTransferFileData.Columns[2] as DataGridTemplateColumn; // FrameworkElement s = dgTransferFileData.Columns[2].GetCellContent(item); // System.Windows.Controls.TextBox t = templeColumn.CellTemplate.FindName("realColumn", s) as System.Windows.Controls.TextBox; // if (reportColumns.FirstOrDefault(x => x.reportcolumn_id.Equals(t.Tag.ToString())) != null) // { // t.Text = reportColumns.FirstOrDefault(x => x.reportcolumn_id.Equals(t.Tag.ToString())).reportcolumn_sname; // } // else // { // } //} tbxCommandName.Text = name; tbxFileName.Text = filename; textboxDestPath.Text = filePath; if(filePostfix== ".xls") { radioBtnXls.IsChecked = true; } else if(filePostfix== ".csv") { radioBtnCsv.IsChecked = true; } //for (int i = 0; i < dgTransferFileData.Items.Count; i++) //{ // DataGridRow dataGridRow = (DataGridRow)dgTransferFileData.ItemContainerGenerator.ContainerFromIndex(i); // var cbox = dgTransferFileData.Columns[0].GetCellContent(dataGridRow); // setVisualChild(cbox); //} //foreach (DropdownName item in dgTransferFileData.Items) //{ // DataGridTemplateColumn templeColumn = dgTransferFileData.Columns[0] as DataGridTemplateColumn; // FrameworkElement s = dgTransferFileData.Columns[0].GetCellContent(item); // System.Windows.Controls.CheckBox cb = templeColumn.CellTemplate.FindName("realCheck", s) as System.Windows.Controls.CheckBox; // if (reportColumns.FirstOrDefault(x => x.reportcolumn_id.Equals(cb.Tag.ToString())) != null) // { // cb.IsChecked = true; // } // else // { // cb.IsChecked = false; // } //} //for (int i = 0; i < dgTransferFileData.Items.Count; i++) //{ // DataGridRow dataGridRow = (DataGridRow)dgTransferFileData.ItemContainerGenerator.ContainerFromIndex(i); // var tbox = dgTransferFileData.Columns[2].GetCellContent(dataGridRow); // setVisualChildTextbox(tbox); //} List reportColumns = methodDataExport.reportColumns; if (reportColumns != null) { for (int j = 0; j < checkBoxes.Length; j++) { var a = reportColumns.SingleOrDefault(x => x.reportcolumn_id.Equals((j + 1).ToString())); if (a != null) { checkBoxes[j].IsChecked = true; tboxs[j].Text = a.reportcolumn_sname; } else { checkBoxes[j].IsChecked = false; } } } checkedHeaderByClickResult(); if (currmethodDataExport != null) { methodDataExport = currmethodDataExport; } } else { if (methodDataExport == null) return; if (string.Compare(methodDataExport.filePostfix, ".csv", true) == 0) radioBtnCsv.IsChecked = true; else radioBtnXls.IsChecked = true; if (currmethodDataExport != null) { methodDataExport = currmethodDataExport; } } } catch (Exception ex) { LoggerHelper.ErrorLog("ERROR:", ex); } } #region 查找控件的子控件,并返回子控件对象 public List GetChildObjects(DependencyObject obj, Type typename) where T : FrameworkElement { DependencyObject child = null; List childList = new List(); for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++) { child = VisualTreeHelper.GetChild(obj, i); if (child is T && (((T)child).GetType() == typename)) { childList.Add((T)child); } childList.AddRange(GetChildObjects(child, typename)); } return childList; } #endregion private void btnSelectFolder_Click(object sender, RoutedEventArgs e) { var dialog = new FolderBrowserDialog(); var result = dialog.ShowDialog(); if(result== DialogResult.OK) { if (methodDataExport != null) { methodDataExport.filePath = dialog.SelectedPath; } textboxDestPath.Text= dialog.SelectedPath; } } private void radioBtnXls_Checked(object sender, RoutedEventArgs e) { if(methodDataExport==null) return; if (methodDataExport != null) { methodDataExport.filePostfix = ".xls"; } } private void radioBtnCsv_Checked(object sender, RoutedEventArgs e) { if (methodDataExport == null) return; if (methodDataExport != null) { methodDataExport.filePostfix = ".csv"; } } private void ckbSelectedAll_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < checkBoxes.Count(); i++) { if(ckbSelectedAll.IsChecked==true) { checkBoxes[i].IsChecked = true; } else { checkBoxes[i].IsChecked = false; } } } public void GetVisualChild(DependencyObject parent) { int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for(int i=0;ix.reportcolumn_id.Equals(child.Tag.ToString()))!=null) { child.IsChecked = true; } else { child.IsChecked = false; } return; } } } public void setVisualChildTextbox(DependencyObject parent) { int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent, i); System.Windows.Controls.TextBox child = v as System.Windows.Controls.TextBox; if (child == null) { setVisualChildTextbox(v); } else { var a = reportColumns.FirstOrDefault(x => x.reportcolumn_id.Equals(child.Tag.ToString())); if (a != null) { child.Text = a.reportcolumn_sname; } else { } return; } } } private void tbxCommandName_TextChanged(object sender, TextChangedEventArgs e) { try { if (string.IsNullOrEmpty(tbxCommandName.Text)) { xcError.Text = "命令名称不能为空"; return; } if (methodDataExport != null) { methodDataExport.name = tbxCommandName.Text; } } catch (Exception ex) { LoggerHelper.ErrorLog("ERROR:", ex); } } private void tbxFileName_TextChanged(object sender, TextChangedEventArgs e) { try { if (methodDataExport != null) { methodDataExport.fileName = tbxFileName.Text; } } catch (Exception ex) { LoggerHelper.ErrorLog("ERROR:", ex); } } private void textboxDestPath_TextChanged(object sender, TextChangedEventArgs e) { try { if (methodDataExport != null) { methodDataExport.filePath = textboxDestPath.Text; } } catch (Exception ex) { LoggerHelper.ErrorLog("ERROR:", ex); } } private void realCheck_Click(object sender, RoutedEventArgs e) { System.Windows.Controls.CheckBox cb = sender as System.Windows.Controls.CheckBox; if(cb!=null) { if(cb.IsChecked== true) { if(reportColumns.FirstOrDefault(x=>x.reportcolumn_id.Equals(cb.Tag.ToString()))!=null) { } else { ReportColumn rc = new ReportColumn(); rc.reportcolumn_id = cb.Tag.ToString(); var a = dropdownNames.FirstOrDefault(x => x.dropdown_id.Equals(cb.Tag.ToString())); rc.reportcolumn_name = a.dropdown_name; rc.reportcolumn_sname = ""; reportColumns.Add(rc); } } else { } } } private void realColumn_TextChanged(object sender, TextChangedEventArgs e) { try { System.Windows.Controls.TextBox tb = sender as System.Windows.Controls.TextBox; if (tb != null) { var a = reportColumns.FirstOrDefault(x => x.reportcolumn_id.Equals(tb.Tag.ToString())); if (a != null) { a.reportcolumn_sname = tb.Text; } else { } } } catch (Exception ex) { LoggerHelper.ErrorLog("ERROR:", ex); } } private void dgTransferFileData_Loaded(object sender, RoutedEventArgs e) { } private void dgTransferFileData_Initialized(object sender, EventArgs e) { //for (int i = 0; i < dgTransferFileData.Items.Count; i++) //{ // DataGridRow dataGridRow = (DataGridRow)dgTransferFileData.ItemContainerGenerator.ContainerFromIndex(i); // var tbox = dgTransferFileData.Columns[2].GetCellContent(dataGridRow); // setVisualChildTextbox(tbox); //} } System.Windows.Controls.CheckBox[] checkBoxes; System.Windows.Controls.TextBox[] tboxs; private void BindReportColumn() { //dropdownNames = DataExportBll.GenerateReportColumn(); gdReportColList.Children.Clear(); gdReportColList.RowDefinitions.Clear(); gdReportColList.ColumnDefinitions.Clear(); ColumnDefinition cd = new ColumnDefinition(); cd.Width = new GridLength(50); gdReportColList.ColumnDefinitions.Add(cd); cd = new ColumnDefinition(); cd.Width = new GridLength(50); gdReportColList.ColumnDefinitions.Add(cd); cd = new ColumnDefinition(); cd.Width = new GridLength(120); gdReportColList.ColumnDefinitions.Add(cd); cd = new ColumnDefinition(); cd.Width = new GridLength(120); gdReportColList.ColumnDefinitions.Add(cd); RowDefinition rd = new RowDefinition(); rd.Height = new GridLength(30); gdReportColList.RowDefinitions.Add(rd); TextBlock tb = new TextBlock(); tb.Text = ""; gdReportColList.Children.Add(tb); tb.SetValue(Grid.RowProperty, 0); tb.SetValue(Grid.ColumnProperty, 0); tb = new TextBlock(); tb.Text = "序号"; gdReportColList.Children.Add(tb); tb.SetValue(Grid.RowProperty, 0); tb.SetValue(Grid.ColumnProperty, 1); tb = new TextBlock(); tb.Text = "字段名"; gdReportColList.Children.Add(tb); tb.SetValue(Grid.RowProperty, 0); tb.SetValue(Grid.ColumnProperty, 2); tb = new TextBlock(); tb.Text = "列名"; gdReportColList.Children.Add(tb); tb.SetValue(Grid.RowProperty, 0); tb.SetValue(Grid.ColumnProperty, 3); checkBoxes = new System.Windows.Controls.CheckBox[dropdownNames.Count]; tboxs = new System.Windows.Controls.TextBox[dropdownNames.Count]; for (int i = 0; i < dropdownNames.Count; i++) { rd = new RowDefinition(); rd.Height = new GridLength(30); gdReportColList.RowDefinitions.Add(rd); checkBoxes[i] = new System.Windows.Controls.CheckBox(); checkBoxes[i].IsChecked = true; checkBoxes[i].Tag = i; gdReportColList.Children.Add(checkBoxes[i]); checkBoxes[i].SetValue(Grid.RowProperty, i + 1); checkBoxes[i].SetValue(Grid.ColumnProperty, 0); checkBoxes[i].AddHandler(System.Windows.Controls.CheckBox.ClickEvent, new RoutedEventHandler(check_Click)); TextBlock tbk = new TextBlock(); tbk.Text = dropdownNames[i].dropdown_id; tbk.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; gdReportColList.Children.Add(tbk); tbk.SetValue(Grid.RowProperty, i + 1); tbk.SetValue(Grid.ColumnProperty, 1); tbk = new TextBlock(); tbk.Text = dropdownNames[i].dropdown_name; tbk.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; gdReportColList.Children.Add(tbk); tbk.SetValue(Grid.RowProperty, i + 1); tbk.SetValue(Grid.ColumnProperty, 2); tboxs[i] = new System.Windows.Controls.TextBox(); tboxs[i].Text = dropdownNames[i].dropdown_name; tboxs[i].AddHandler(System.Windows.Controls.TextBox.TextChangedEvent, new TextChangedEventHandler(tbx_TextBoxChanged)); tboxs[i].Tag = i; tboxs[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Left; tboxs[i].VerticalAlignment = VerticalAlignment.Top; tboxs[i].Width = 120; tboxs[i].Height = 30; tboxs[i].Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFF2F3F5")); gdReportColList.Children.Add(tboxs[i]); tboxs[i].SetValue(Grid.RowProperty, i + 1); tboxs[i].SetValue(Grid.ColumnProperty, 3); } } #region 单个选中 private void check_Click(object sender, RoutedEventArgs e) { System.Windows.Controls.CheckBox cbx = (System.Windows.Controls.CheckBox)sender; if (cbx.IsChecked.Equals(true)) { if (methodDataExport != null) { if (methodDataExport.reportColumns != null) { var a = methodDataExport.reportColumns.SingleOrDefault(x => x.reportcolumn_id == ((int)cbx.Tag + 1).ToString()); if (a == null) { ReportColumn reportColumn = new ReportColumn(); reportColumn.reportcolumn_id = ((int)cbx.Tag + 1).ToString(); reportColumn.reportcolumn_name = dropdownNames[(int)cbx.Tag].dropdown_name; reportColumn.reportcolumn_sname = tboxs[(int)cbx.Tag].Text; methodDataExport.reportColumns.Add(reportColumn); } } } } else { if (methodDataExport != null) { if (methodDataExport.reportColumns != null) { var a = methodDataExport.reportColumns.SingleOrDefault(x => x.reportcolumn_id == ((int)cbx.Tag + 1).ToString()); if (a != null) { methodDataExport.reportColumns.Remove(a); } } } } checkedHeaderByClickResult(); } #endregion #region 检查当前是否已经全部勾选,如果是则把表头的也勾选;否则不勾选 private void checkedHeaderByClickResult() { int total = 0; for (int i = 0; i < checkBoxes.Count(); i++) { if (checkBoxes[i].IsChecked == true) { total++; } else { } } if(total==8) { ckbSelectedAll.IsChecked= true; } else { ckbSelectedAll.IsChecked = false; } } #endregion private void tbx_TextBoxChanged(object sender, TextChangedEventArgs e) { System.Windows.Controls.TextBox tb = (System.Windows.Controls.TextBox)sender; if (checkBoxes[(int)tb.Tag].IsChecked.Equals(true)) { if (methodDataExport != null&& methodDataExport.reportColumns!=null) { int a = methodDataExport.reportColumns.FindLastIndex(x => x.reportcolumn_id == ((int)tb.Tag + 1).ToString()); methodDataExport.reportColumns[a].reportcolumn_sname = tb.Text; } } } } }