using SunlightAggregationManager.View; using SunlightAggregationManager.ViewModel; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Xml.Linq; namespace SunlightAggregationManager.UserWindow { /// /// Machine.xaml 的交互逻辑 /// public partial class User : Window { CheckBox[] checkBoxes = new CheckBox[99]; string[] strings = new string[8] {"修改系统设置", "修改用户设置", "设备监控","设备控制","远程设置","远程查询","输送信息","历史查询"}; public User() { WindowStartupLocation = WindowStartupLocation.CenterScreen; InitializeComponent(); } private void user_Loaded(object sender, RoutedEventArgs e)//打开页面执行 { for (int i = 0; i < strings.Length; i++) { checkBoxes[i] = new CheckBox(); checkBoxes[i].Content = strings[i].ToString(); checkBoxes[i].FontSize = 20; checkBoxes[i].Width = 200; checkBoxes[i].Height = 50; Capacity.Children.Add(checkBoxes[i]); } DataGriduser.ItemsSource = MainWindowViewModel._userData.DefaultView; } private void Tb_KeyFloating(object sender, TextCompositionEventArgs e)//输入事件 { //Regex re = new Regex("[^0-9.-]+"); Regex re = new Regex(@"^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");// 非负浮点数 e.Handled = !re.IsMatch(e.Text); } private void DataGridMac_MouseDoubleClick(object sender, MouseButtonEventArgs e)//数据表双击事件 { string Capacity_; int rownum = DataGriduser.SelectedIndex;//获取鼠标选中行并定义变量 if (rownum != -1)//判断鼠标定位是否有效 { /*定位选中行及指定列单元格文本信息*/ _Name.Text = (DataGriduser.Columns[1].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim();//定位第列 _User.Text = (DataGriduser.Columns[0].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim();//定位第列 GROUP.Text = (DataGriduser.Columns[4].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim(); Capacity_ = (DataGriduser.Columns[5].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim(); _Department.Text = (DataGriduser.Columns[3].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim(); Pasword.Text = (DataGriduser.Columns[2].GetCellContent(DataGriduser.Items[rownum]) as TextBlock)!.Text.Trim(); if (strings.Length > Capacity_.Length) { for (int i = 0; i < Capacity_.Length; i++) { if (Capacity_.Substring(i, 1) == "1") { checkBoxes[i].IsChecked = true; } else { checkBoxes[i].IsChecked = false; } } } else { for (int i = 0; i < strings.Length; i++) { if (Capacity_.Substring(i, 1) == "1") { checkBoxes[i].IsChecked = true; } else { checkBoxes[i].IsChecked = false; } } } } } private void Button_Preservation(object sender, RoutedEventArgs e)//保存按钮事件 { Regex re_number = new Regex(@"^[0-9]+(.[0-9]{1,2})?$");//校验用正则表达式有1~2位小数的正实数 Regex re_char = new Regex(@"^[A-Za-z0-9\s@()()/+!!_-]+$");//校验用正则表达式由数字,26个英文字母,空白字符和@()()/+!!_-组成的字符串 string user = _User.Text; string name = _Name.Text; string password = Pasword.Text; string Group = GROUP.Text; string Cap = ""; string department = _Department.Text; for (int i = 0; i < strings.Length; i++) { if ((bool)checkBoxes[i].IsChecked!) { Cap = Cap + "1"; } else { Cap = Cap + "0"; } } Dictionary USER_new = new Dictionary();//缓存函数 USER_new.Add("User", user); USER_new.Add("Name", name); USER_new.Add("Password", password); USER_new.Add("Groups", Group); USER_new.Add("Capacity", Cap); USER_new.Add("Department", department); USER_new.Add("State", 0); USER_new.Add("Enterprise", MainWindowViewModel.Enterprise); var rowdata = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData, "UserID", "User='" + user + "'"); if (rowdata != null) { MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + user + "'", USER_new); } else { MainWindowViewModel.Insert_Memory(MainWindowViewModel._userData, USER_new);// 执行插入 } DataGriduser.ItemsSource = MainWindowViewModel._userData.DefaultView; _Name.Text = null; } private void Button_Delete(object sender, RoutedEventArgs e)//删除按钮事件 { try { MainWindowViewModel.Delete_Memory(MainWindowViewModel._userData, "User='" + _User.Text + "'"); } catch (Exception) { } finally { DataGriduser.ItemsSource = MainWindowViewModel._userData.DefaultView; _Name.Text = null; } } } }