using DevExpress.XtraBars; using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; using DevExpress.XtraEditors.Repository; using DevExpress.XtraLayout.Utils; using DevExpress.XtraVerticalGrid.Events; namespace Yw.WinFrmUI { public partial class HydroVisualPropertyCtrl : XtraUserControl { public HydroVisualPropertyCtrl() { InitializeComponent(); this.layoutControl1.SetupLayoutControl(); } /// /// 匹配db事件 /// 第一个参数为匹配Db的ViewModel /// 第二个参数为是否匹配成功 /// public event Func MatchingDbEvent; /// /// 设置曲线事件 /// 第一个参数为设置曲线的ViewModel /// 第二个参数为曲线类型 /// 第三个参数为是否设置成功 /// public event Func SetCurveEvent; /// /// 设置模式事件 /// 第一个参数为设置模式事的ViewModel /// 第二个参数为模式事类型 /// 第三个参数为是否设置成功 /// public event Func SetPatternEvent; /// /// 属性值发生改变事件 /// public event Action PropertyValueChangedEvent; /// /// 标注属性值改变事件 /// public event Action MarkPropertyValueChangedEvent; /// /// 分级属性值改变事件 /// public event Action GradingPropertyValueChangedEvent; /// /// 水流动画属性值改变事件 /// public event Action FlowEffectPropertyValueChangedEvent; /// /// 强调连接节点事件 /// 第一个参数为本身Code /// 第二个参数为连接节点Code /// public event Action BlinkLinkNodeEvent; /// /// 查看构件事件 /// public event Action HydroViewEvent; /// /// 绑定对象 /// public HydroVisualViewModel SelectedObject { get { var vm = this.propertyGridControl1.SelectedObject as HydroVisualViewModel; return vm; } set { this.barBtnChangeLink.Visibility = value is HydroLinkViewModel ? BarItemVisibility.Always : BarItemVisibility.Never; this.barBtnView.Visibility = value == null ? BarItemVisibility.Never : BarItemVisibility.Always; this.propertyGridControl1.SelectedObject = value; } } /// /// 重新从数据源中读取数据,外观恢复刚开始加载的样子 /// public void UpdateData() { this.propertyGridControl1.UpdateData(); } /// /// 更新数据,样式不变 /// public void UpdateRows() { this.propertyGridControl1.UpdateRows(); } //获取行的视图对象 private HydroVisualViewModel GetPropertyViewModel(DevExpress.XtraVerticalGrid.Rows.BaseRow row) { if (row == null) { return default; } if (row.Level <= 1) { return this.SelectedObject; } if (row.ParentRow == null) { return this.SelectedObject; } if (row.ParentRow.Properties.Value is HydroVisualViewModel) { return row.ParentRow.Properties.Value as HydroVisualViewModel; } return GetPropertyViewModel(row.ParentRow); } //设置描述控件的可见性 private void SetDescriptionVisible(bool isVisible) { var visible = isVisible ? LayoutVisibility.Always : LayoutVisibility.Never; this.layoutDescription.Visibility = this.splitterItem1.Visibility = visible; } //自定义属性Header缩进 private void propertyGridControl1_CustomDrawRowHeaderIndent(object sender, CustomDrawRowHeaderIndentEventArgs e) { } //自定义属性Header显示 private void propertyGridControl1_CustomDrawRowHeaderCell(object sender, CustomDrawRowHeaderCellEventArgs e) { //字段名称 var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last(); if (string.IsNullOrEmpty(fieldName)) { return; } //属性描述器 var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row); if (descriptor != null) { //名称 var displayNameAttri = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)]; if (displayNameAttri != null && !string.IsNullOrEmpty(displayNameAttri.DisplayName)) { e.Caption = displayNameAttri.DisplayName; } } //颜色 var vm = GetPropertyViewModel(e.Row); if (vm != null) { var propStatus = vm.GetPropStatus(fieldName); if (propStatus != null) { switch (propStatus.PropStatus) { case Yw.Hydro.ePropStatus.Error: { e.Appearance.ForeColor = Color.Red; } break; case Yw.Hydro.ePropStatus.Normal: { e.Appearance.ForeColor = Color.Black; } break; case Yw.Hydro.ePropStatus.Lack: { e.Appearance.ForeColor = Color.Green; } break; case Yw.Hydro.ePropStatus.Abnormal: { e.Appearance.ForeColor = Color.Orange; } break; default: break; } } } } //自定义属性值显示 private void propertyGridControl1_CustomDrawRowValueCell(object sender, CustomDrawRowValueCellEventArgs e) { //行字段名称 var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last(); if (string.IsNullOrEmpty(fieldName)) { return; } //行类型全名称 var fullTypeName = e.Row.Properties.RowType.FullName; if (fullTypeName == typeof(DateTime).FullName) { e.CellText = ((DateTime)e.Properties.Value).ToString("yyyy-MM-dd HH:mm:ss"); } else { var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row); if (descriptor != null) { var calcuPropAttri = (HydroCalcuProAttribute)descriptor.Attributes[typeof(HydroCalcuProAttribute)]; if (calcuPropAttri != null) { if (e.Properties.Value == null) { e.CellText = "尚未计算"; e.Appearance.ForeColor = Color.OrangeRed; } } var displayUnitAttri = (DisplayUnitAttribute)descriptor.Attributes[typeof(DisplayUnitAttribute)]; if (displayUnitAttri != null) { if (e.Properties.Value != null) { e.CellText = e.Properties.Value.ToString() + " " + displayUnitAttri.Unit; } } } } } //属性编辑框的显示与取消 private void propertyGridControl1_ShowingEditor(object sender, CancelEventArgs e) { var rowTypeFullName = this.propertyGridControl1.FocusedRow.Properties.RowType.FullName; var fieldName = this.propertyGridControl1.FocusedRow.Properties.FieldName.Split(new char[] { '.' }).Last(); if (rowTypeFullName == typeof(Image).FullName) { e.Cancel = true; return; } var descriptor = this.propertyGridControl1.GetPropertyDescriptor(this.propertyGridControl1.FocusedRow); var showEditor = (ShowEditorAttribute)descriptor.Attributes[typeof(ShowEditorAttribute)]; if (showEditor != null) { if (!showEditor.ShowEditor) { e.Cancel = true; return; } } } //编辑框 private void propertyGridControl1_CustomRecordCellEdit(object sender, GetCustomRowCellEditEventArgs e) { var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last(); if (string.IsNullOrEmpty(fieldName)) { return; } var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row); var rowTypeFullName = e.Row.Properties.RowType.FullName; var vm = GetPropertyViewModel(e.Row); if (vm == null) { return; } #region bool if (rowTypeFullName == typeof(bool).FullName) { var ckEdit = new RepositoryItemCheckEdit(); ckEdit.CheckStyle = DevExpress.XtraEditors.Controls.CheckStyles.Standard; if (e.Row.Properties.ReadOnly == true) { ckEdit.ReadOnly = true; } e.RepositoryItem = ckEdit; } #endregion #region 富文本 var attri_multi = (MultiTextAttribute)descriptor.Attributes[typeof(MultiTextAttribute)]; if (attri_multi != null) { var memoEdit = new RepositoryItemMemoEdit(); if (e.Row.Properties.ReadOnly == true) { memoEdit.ReadOnly = true; } e.RepositoryItem = memoEdit; } #endregion #region 图片 if (rowTypeFullName == typeof(Image).FullName) { var picEdit = new RepositoryItemPictureEdit(); picEdit.ReadOnly = true; picEdit.NullText = "空"; e.RepositoryItem = picEdit; e.Row.Expanded = true; } #endregion #region 型号 var modelTypeAttri = (HydroModelTypeProAttribute)descriptor.Attributes[typeof(HydroModelTypeProAttribute)]; if (modelTypeAttri != null) { var buttonEdit = new RepositoryItemButtonEdit(); buttonEdit.TextEditStyle = TextEditStyles.DisableTextEditor; buttonEdit.ButtonClick += delegate { if (this.MatchingDbEvent == null) { return; } var vm = GetPropertyViewModel(e.Row); if (vm == null) { return; } var bol = this.MatchingDbEvent.Invoke(vm); if (bol) { UpdateRows(); } }; e.RepositoryItem = buttonEdit; } #endregion #region 曲线 var curvePropAttri = (HydroCurveProAttribute)descriptor.Attributes[typeof(HydroCurveProAttribute)]; if (curvePropAttri != null) { var buttonEdit = new RepositoryItemButtonEdit(); buttonEdit.TextEditStyle = TextEditStyles.DisableTextEditor; buttonEdit.ButtonClick += delegate { var vm = GetPropertyViewModel(e.Row); if (vm == null) { return; } switch (curvePropAttri.CurveType) { case HydroCurve.Tank: { if (this.SetCurveEvent == null || !this.SetCurveEvent.Invoke(vm, HydroCurve.Pump)) { XtraMessageBox.Show("正在开发中..."); } } break; case HydroCurve.TankVol: { if (this.SetCurveEvent == null || !this.SetCurveEvent.Invoke(vm, HydroCurve.TankVol)) { XtraMessageBox.Show("正在开发中..."); } } break; case HydroCurve.Pump: { if (this.SetCurveEvent == null || !this.SetCurveEvent.Invoke(vm, HydroCurve.Pump)) { //缺省的设置方法 } } break; case HydroCurve.PumpQH: { if (this.SetCurveEvent == null || !this.SetCurveEvent.Invoke(vm, HydroCurve.PumpQH)) { var pumpViewModel = vm as HydroPumpViewModel; var curveQh = vm.HydroInfo.Curves?.Find(x => x.Code == pumpViewModel.CurveQH); var dlg = new SetHydroCurveDlg(); dlg.ReloadDataEvent += (curveInfo) => { if (vm.HydroInfo.Curves == null) { vm.HydroInfo.Curves = new List(); } if (curveQh != null) { vm.HydroInfo.Curves.Remove(curveQh); } vm.HydroInfo.Curves.Add(curveInfo); pumpViewModel.CurveQH = curveInfo.Code; UpdateRows(); }; dlg.SetBindingData(vm.HydroInfo, curveQh, curvePropAttri.CurveType); dlg.ShowDialog(); } } break; case HydroCurve.PumpQP: { if (this.SetCurveEvent == null || !this.SetCurveEvent.Invoke(vm, curvePropAttri.CurveType)) { var pumpViewModel = vm as HydroPumpViewModel; var curveQp = vm.HydroInfo.Curves?.Find(x => x.Code == pumpViewModel.CurveQP); var dlg = new SetHydroCurveDlg(); dlg.ReloadDataEvent += (curveInfo) => { if (vm.HydroInfo.Curves == null) { vm.HydroInfo.Curves = new List(); } if (curveQp != null) { vm.HydroInfo.Curves.Remove(curveQp); } vm.HydroInfo.Curves.Add(curveInfo); pumpViewModel.CurveQP = curveInfo.Code; UpdateRows(); }; dlg.SetBindingData(vm.HydroInfo, curveQp, curvePropAttri.CurveType); dlg.ShowDialog(); } } break; case HydroCurve.PumpQE: { var pumpViewModel = vm as HydroPumpViewModel; var curveQe = vm.HydroInfo.Curves?.Find(x => x.Code == pumpViewModel.CurveQE); var dlg = new SetHydroCurveDlg(); dlg.ReloadDataEvent += (curveInfo) => { if (vm.HydroInfo.Curves == null) { vm.HydroInfo.Curves = new List(); } if (curveQe != null) { vm.HydroInfo.Curves.Remove(curveQe); } vm.HydroInfo.Curves.Add(curveInfo); pumpViewModel.CurveQE = curveInfo.Code; UpdateRows(); }; dlg.SetBindingData(vm.HydroInfo, curveQe, curvePropAttri.CurveType); dlg.ShowDialog(); } break; case HydroCurve.Valve: { if (this.SetCurveEvent == null || !this.SetCurveEvent.Invoke(vm, HydroCurve.Valve)) { //缺省的设置方法 } } break; case HydroCurve.ValveQL: { if (this.SetCurveEvent == null || !this.SetCurveEvent.Invoke(vm, curvePropAttri.CurveType)) { //缺省的设置方法 } } break; default: break; } }; e.RepositoryItem = buttonEdit; } #endregion #region 模式 var patternProAttri = (HydroPatternProAttribute)descriptor.Attributes[typeof(HydroPatternProAttribute)]; if (patternProAttri != null) { var buttonEdit = new RepositoryItemButtonEdit(); buttonEdit.TextEditStyle = TextEditStyles.DisableTextEditor; buttonEdit.ButtonClick += delegate { var vm = GetPropertyViewModel(e.Row); switch (patternProAttri.PatternType) { case HydroPattern.Head: { if (this.SetPatternEvent == null || !this.SetPatternEvent.Invoke(vm, HydroPattern.Head)) { //缺省的设置方法 } } break; case HydroPattern.Demand: { if (this.SetPatternEvent == null || !this.SetPatternEvent.Invoke(vm, HydroPattern.Demand)) { //缺省的设置方法 } } break; } }; e.RepositoryItem = buttonEdit; } #endregion #region 标签 var flagsProAttri = (HydroFlagsProAttribute)descriptor.Attributes[typeof(HydroFlagsProAttribute)]; if (flagsProAttri != null) { var buttonEdit = new RepositoryItemButtonEdit(); buttonEdit.TextEditStyle = TextEditStyles.DisableTextEditor; buttonEdit.ButtonClick += async delegate { var vm = GetPropertyViewModel(e.Row); var flagVmoList = await BLLFactory.Instance.GetBySysType(Yw.Hydro.DataType.HydroParter); var dbFlagList = flagVmoList?.Select(x => x.Name).Distinct().ToList(); var dlg = new SetFlagsDlg(); dlg.SetBindingData(dbFlagList, vm.Vmo.Flags); dlg.ReloadDataEvent += (flags) => { vm.Vmo.Flags = flags; vm.Flags = Yw.Untity.FlagsHelper.ToString(flags); UpdateRows(); this.PropertyValueChangedEvent?.Invoke(vm); }; dlg.ShowDialog(); }; e.RepositoryItem = buttonEdit; } #endregion #region 连接 if (e.Row.Properties.Value != null) { var linkCodeAttri = (HydroLinkProAttribute)descriptor.Attributes[typeof(HydroLinkProAttribute)]; if (linkCodeAttri != null) { var buttonEdit = new RepositoryItemButtonEdit(); buttonEdit.TextEditStyle = TextEditStyles.DisableTextEditor; buttonEdit.ButtonClick += delegate { var vm = GetPropertyViewModel(e.Row); var vmLink = vm as HydroLinkViewModel; this.BlinkLinkNodeEvent?.Invoke(vmLink, e.Row.Properties.Value.ToString()); }; e.RepositoryItem = buttonEdit; } } #endregion } //属性值改变 private void propertyGridControl1_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e) { var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last(); if (string.IsNullOrEmpty(fieldName)) { return; } var vm = GetPropertyViewModel(e.Row); if (vm == null) { return; } vm.UpdateVmoProperty(); this.PropertyValueChangedEvent?.Invoke(vm); var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row); if (descriptor != null) { var hydroMarkPropAttri = descriptor.Attributes[typeof(HydroMarkProAttribute)]; if (hydroMarkPropAttri != null) { this.MarkPropertyValueChangedEvent?.Invoke(vm); } var hydroGradingPropAttri = descriptor.Attributes[typeof(HydroGradingProAttribute)]; if (hydroGradingPropAttri != null) { this.GradingPropertyValueChangedEvent?.Invoke(vm); } var hydroFlowDirectionPropAttri = descriptor.Attributes[typeof(HydroFlowDirectionProAttribute)]; if (hydroFlowDirectionPropAttri != null) { this.FlowEffectPropertyValueChangedEvent?.Invoke(vm); } } } //改变连接节点 private void barBtnChangeLink_ItemClick(object sender, ItemClickEventArgs e) { var link = this.SelectedObject as HydroLinkViewModel; if (link != null) { var tempCode = link.StartCode; link.StartCode = link.EndCode; link.EndCode = tempCode; link.UpdateVmoProperty(); this.propertyGridControl1.UpdateRows(); } } //全部展开 private void barBtnExpandAll_ItemClick(object sender, ItemClickEventArgs e) { this.propertyGridControl1.ExpandAllRows(); } //全部折叠 private void barBtnCollpseAll_ItemClick(object sender, ItemClickEventArgs e) { this.propertyGridControl1.CollapseAllRows(); } //查看 private void barBtnView_ItemClick(object sender, ItemClickEventArgs e) { var vm = this.SelectedObject; if (vm == null) { return; } this.HydroViewEvent?.Invoke(vm); } // 描述 private void barBtnHelp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { var visible = this.splitterItem1.Visibility == LayoutVisibility.Always ? false : true; SetDescriptionVisible(visible); } //聚焦行改变 private void propertyGridControl1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e) { if (e.Row == null) { return; } //字段名称 var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last(); if (string.IsNullOrEmpty(fieldName)) { return; } var vm = GetPropertyViewModel(e.Row); if (vm == null) { return; } string caption = string.Empty; string descrition = string.Empty; //属性描述器 var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row); if (descriptor != null) { //名称 var displayNameAttri = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)]; if (displayNameAttri != null && !string.IsNullOrEmpty(displayNameAttri.DisplayName)) { caption = displayNameAttri.DisplayName; } //描述 var descritionAttri = (DescriptionAttribute)descriptor.Attributes[typeof(DescriptionAttribute)]; if (descritionAttri != null) { descrition = descritionAttri.Description; } } var propStatus = vm.GetPropStatus(fieldName); this.hydroVisualPropertyDescriptionCtrl1.SetBindingData(caption, descrition, propStatus); } } }