/// <summary> /// 当鼠标点击树形列表右键菜单的选项时发生。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MenuItem_Root_Click(object sender, RoutedEventArgs e) { MenuItem item = sender as MenuItem; NodeInfo nodeInfo = item.Tag as NodeInfo; ConnectionConfig config = GlobalBusiness.DictConnectionConfig[nodeInfo.ConfigId]; if (MenuItemType.OPEN.Equals(nodeInfo.Type)) { if (this.currentSelectedTreeViewItem.HasItems) { return; } this.CreateDBNode(this.currentSelectedTreeViewItem); this.currentSelectedTreeViewItem.IsExpanded = true; return; } if (MenuItemType.REFRESH.Equals(nodeInfo.Type)) { this.currentSelectedTreeViewItem.Items.Clear(); this.CreateDBNode(this.currentSelectedTreeViewItem); this.currentSelectedTreeViewItem.IsExpanded = true; return; } if (MenuItemType.EDIT.Equals(nodeInfo.Type)) { if (this.currentSelectedTreeViewItem.HasItems && MessageBox.Show("该操作将关闭连接!是否继续?", "操作提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { this.currentSelectedTreeViewItem.Items.Clear(); } WinAddConnection winAddConnection = new WinAddConnection(nodeInfo.ConfigId, ConfigOperationType.EDIT); winAddConnection.SavedConnectionConfig += this.WinAddConnection_SavedConnectionConfig; winAddConnection.ShowDialog(); winAddConnection.SavedConnectionConfig -= this.WinAddConnection_SavedConnectionConfig; return; } if (MenuItemType.DELETE.Equals(nodeInfo.Type)) { if (MessageBox.Show($"确定删除{config.Name}连接配置吗?", "删除提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { TreeView tree = this.currentSelectedTreeViewItem.Parent as TreeView; this.gridRedisList.Children.Remove(tree); GlobalBusiness.RemoveConfig(config.Id); } return; } if (MenuItemType.CLOSE.Equals(nodeInfo.Type)) { this.currentSelectedTreeViewItem.Items.Clear(); this.currentSelectedTreeViewItem.IsExpanded = false; return; } }
/// <summary> /// 初始化连接信息树。 /// </summary> private void InitTree() { List <ConnectionConfig> configs = GlobalBusiness.GetAllConnectionConfig(); foreach (ConnectionConfig config in configs) { TreeView tree = this.CreateRootNode(config); this.gridRedisList.Children.Add(tree); } }
/// <summary> /// 当鼠标点击保存按钮时发生。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSave_Click(object sender, RoutedEventArgs e) { try { AssertUtil.FormDataValidate("名称不能为空。", () => { return(string.IsNullOrWhiteSpace(this.tbConnName.Text)); }); AssertUtil.FormDataValidate("地址不能为空。", () => { return(string.IsNullOrWhiteSpace(this.tbConnIP.Text)); }); AssertUtil.FormDataValidate("端口不能为空。", () => { return(string.IsNullOrWhiteSpace(this.tbConnPort.Text)); }); AssertUtil.FormDataValidate("端口不合法。", () => { return(!Regex.IsMatch(this.tbConnPort.Text, @"^[+-]?\d*$")); }); ConnectionConfig config = new ConnectionConfig() { Name = this.tbConnName.Text.Trim(), IP = this.tbConnIP.Text.Trim(), Port = Convert.ToInt32(this.tbConnPort.Text.Trim()), Password = this.tbConnPassword.Text.Trim() }; if (configOperationType == ConfigOperationType.ADD) { config.Id = Guid.NewGuid().ToString("N"); GlobalBusiness.SaveConfig(config); } else { config.Id = this.configId; GlobalBusiness.UpdateConfig(config); } MessageBox.Show("保存成功!"); this.SavedConnectionConfig?.Invoke(config, this.configOperationType); this.Close(); } catch (IllegalFormDataException ex) { MessageBox.Show(ex.Message); } catch (DuplicateMemberException ex) { MessageBox.Show(ex.Message); } catch (KeyNotFoundException ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 使用指定的参数创建实例。 /// </summary> /// <param name="connId">连接名称。</param> public WinAddConnection(string connId, ConfigOperationType configOperationType) { InitializeComponent(); this.WindowStartupLocation = WindowStartupLocation.CenterScreen; this.IsShowHelp = false; this.configOperationType = configOperationType; if (!string.IsNullOrWhiteSpace(connId)) { this.configId = connId; ConnectionConfig config = GlobalBusiness.GetConnectionConfig(connId); this.tbConnIP.Text = config.IP; this.tbConnPort.Text = config.Port.ToString(); this.tbConnName.Text = config.Name; this.tbConnPassword.Text = config.Password; } }