using DevExpress.XtraEditors; using IStation.Untity; using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace IStation.WinFrmUI.Monitor { /// /// /// public partial class MonitorDataSourcesListCtrl : XtraUserControl { public MonitorDataSourcesListCtrl() { InitializeComponent(); this.treeList1.InitialDefaultSettings(); this.treeList1.SelectImageList = ImageLib.Lib; this.layoutControl1.SetupLayoutControl(); } public class CurrentViewModel : Model.MonitorDataSources { public CurrentViewModel() { } public CurrentViewModel(Model.MonitorDataSources rhs) : base(rhs) { this.ImageIndex = ImageLib.MonitorDataSources; } public int ImageIndex { get; set; } } /// /// 聚焦改变事件 /// public event Action FocusedChangedEvent; private BLL.MonitorDataSources _bll = new BLL.MonitorDataSources(); private List _allBindingList = null;//所有绑定列表 /// /// 绑定数据 /// public void Clear() { _allBindingList = new List(); this.treeList1.DataSource = _allBindingList; this.FocusedChangedEvent?.Invoke(null); } /// /// 绑定数据 /// public void SetBindingData() { WaitFrmHelper.ShowWaitForm("正在加载数据..."); _allBindingList = new List(); var list = _bll.GetAll(); if (list != null && list.Any()) { foreach (var item in list) { var vm = new CurrentViewModel(item); _allBindingList.Add(vm); } } this.bindingSource1.DataSource = _allBindingList; WaitFrmHelper.HideWaitForm(); } //全部展开 private void barBtnExpandAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { this.treeList1.ExpandAll(); } //全部折叠 private void barBtnCollapseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { this.treeList1.CollapseAll(); } //检索 private void barCkSearch_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.barCkSearch.Checked) this.treeList1.ShowFindPanel(); else this.treeList1.HideFindPanel(); } //聚焦改变 private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e) { var vm = this.treeList1.GetCurrentViewModel(_allBindingList); if (vm == null) { this.FocusedChangedEvent?.Invoke(null); return; } this.FocusedChangedEvent?.Invoke(vm); } //按钮使用状态 private void SetBarBtnEnabled(bool use) { this.barBtnAdd.Enabled = use; this.barBtnEdit.Enabled = use; this.barBtnDelete.Enabled = use; this.barBtnUpdateSortCode.Enabled = use; this.barBtnDetail.Enabled = use; } //添加 private void barBtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (_allBindingList == null) return; WaitFrmHelper.ShowWaitForm(); var dlg = new AddMonitorDataSourcesDlg(); dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); }; dlg.SetBindingData(); dlg.ReloadDataEvent += (rhs) => { rhs.SortCode = _allBindingList.Count < 1 ? 1 : _allBindingList.Max(x => x.SortCode) + 1; rhs.ID = _bll.Insert(rhs); if (rhs.ID > 0) { _allBindingList.Add(new CurrentViewModel(rhs)); this.bindingSource1.ResetBindings(false); return true; } return false; }; dlg.ShowDialog(); } //编辑 private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.treeList1.GetCurrentViewModel(_allBindingList); if (vm == null) { XtraMessageBox.Show("请选择数据行!"); return; } WaitFrmHelper.ShowWaitForm(); var dlg = new EditMonitorDataSourcesDlg(); dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); }; dlg.SetBindingData(vm); dlg.ReloadDataEvent += (rhs) => { var bol = _bll.Update(rhs); if (bol) { vm.Reset(rhs); this.treeList1.RefreshNode(this.treeList1.FocusedNode); } return bol; }; dlg.ShowDialog(); } //删除 private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.treeList1.GetCurrentViewModel(_allBindingList); if (vm == null) { XtraMessageBox.Show("请选择数据行!"); return; } if (XtraMessageBox.Show($"确认删除数据行?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK) return; var result = _bll.DeleteByID(vm.ID, out string msg); if (result) { if (string.IsNullOrEmpty(msg)) { XtraMessageBox.Show($"删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { XtraMessageBox.Show($"删除成功!\n警告:{msg}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { XtraMessageBox.Show($"删除失败!\n原因:{msg}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _allBindingList.Remove(vm); this.bindingSource1.ResetBindings(false); } //详细信息 private void barBtnDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { XtraMessageBox.Show("待补充!"); } //排序码 private void barBtnUpdateSortCode_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.treeList1.GetCurrentViewModel(_allBindingList); if (vm == null) { XtraMessageBox.Show("请选择数据行!"); return; } var dlg = new SetSortCodeDlg(); dlg.SetBindingData(vm.SortCode); dlg.ReloadDataEvent += (rhs) => { var result = _bll.UpdateSortCode(vm.ID, rhs); if (result) { vm.SortCode = rhs; this.bindingSource1.ResetBindings(false); } return result; }; dlg.ShowDialog(); } //复制 private void barBtnCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var vm = this.treeList1.GetCurrentViewModel(_allBindingList); if (vm == null) { XtraMessageBox.Show("请选择数据行!"); return; } if (XtraMessageBox.Show($"确认复制:{vm.Name}?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK) return; var scene = new Model.MonitorDataSources(vm); scene.ID = 0; scene.Name += "-复制"; scene.SortCode = _allBindingList.Count < 1 ? 1 : _allBindingList.Max(x => x.SortCode) + 1; scene.ID = _bll.Insert(scene); if (scene.ID < 1) { XtraMessageBox.Show("复制失败!"); return; } _allBindingList.Add(new CurrentViewModel(scene)); this.bindingSource1.ResetBindings(false); XtraMessageBox.Show("复制成功!"); } } }