using DevExpress.Data.ExpressionEditor;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraLayout.Utils;
using DevExpress.XtraVerticalGrid.Events;
using NetTaste;
using System.Windows.Forms.VisualStyles;
namespace Yw.WinFrmUI
{
public partial class HydroParterPropertyCtrl : XtraUserControl
{
public HydroParterPropertyCtrl()
{
InitializeComponent();
this.layoutControl1.SetupLayoutControl();
SetDescriptionVisible(false);//默认设置属性描述面板不显示
}
///
/// 获取水力信息事件
///
public event Func GetHydroInfoEvent;
///
/// 选择曲线事件
///
public event Func SelectCurveEvent;
///
/// 选择模式事件
///
public event Func SelectPatternEvent;
///
/// 属性值正在改变事件
///
public event Func PropertyValueChangingEvent;
///
/// 属性值发生改变事件
///
public event Func PropertyValueChangedEvent;
///
/// 绑定对象
///
public HydroParterPropertyViewModel SelectedObject
{
get
{
return this.propertyGridControl1.SelectedObject as HydroParterPropertyViewModel;
}
set
{
this.propertyGridControl1.SelectedObject = value;
OnChanged();
}
}
///
/// 绑定对象列表
///
public List SelectedObjects
{
get { return this.propertyGridControl1.SelectedObjects?.Select(x => x as HydroParterPropertyViewModel).ToList(); }
set
{
this.propertyGridControl1.SelectedObjects = value?.ToArray();
OnChanged();
}
}
//改变触发
protected virtual void OnChanged()
{
}
///
/// 重新从数据源中读取数据,外观恢复刚开始加载的样子
///
public void UpdateData()
{
this.propertyGridControl1.UpdateData();
}
///
/// 更新数据,样式不变
///
public void UpdateRows()
{
this.propertyGridControl1.UpdateRows();
}
//获取行的视图对象
private HydroParterPropertyViewModel GetPropertyViewModel(DevExpress.XtraVerticalGrid.Rows.BaseRow row)
{
if (row.Level == 1)
{
return this.SelectedObject;
}
if (row.ParentRow.Properties.Value is HydroParterPropertyViewModel)
{
return row.ParentRow.Properties.Value as HydroParterPropertyViewModel;
}
return GetPropertyViewModel(row.ParentRow);
}
//设置描述控件的可见性
private void SetDescriptionVisible(bool isVisible)
{
var visible = isVisible ? LayoutVisibility.Always : LayoutVisibility.Never;
this.layoutDescription.Visibility = this.splitterItem1.Visibility = visible;
}
//自定义属性值显示内容
private void propertyGridControl1_CustomDrawRowValueCell(object sender, CustomDrawRowValueCellEventArgs e)
{
var rowTypeFullName = e.Row.Properties.RowType.FullName;
if (rowTypeFullName == typeof(DateTime).FullName)
{
e.CellText = ((DateTime)e.Properties.Value).ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row);
var displayUnit = (DisplayUnitAttribute)descriptor.Attributes[typeof(DisplayUnitAttribute)];
if (displayUnit != null)
{
if (e.Properties.Value != null)
{
e.CellText = e.Properties.Value.ToString() + " " + displayUnit.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();
var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row);
var rowTypeFullName = e.Row.Properties.RowType.FullName;
#region 属性显示名称和描述
//名称
var attri_caption = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)];
if (attri_caption != null && !string.IsNullOrEmpty(attri_caption.DisplayName))
{
e.Row.Properties.Caption = attri_caption.DisplayName;
}
//备注
if (e.Row.Properties.Value == null)
{
e.Row.Properties.ToolTip = string.Empty;
}
else
{
if (e.Row.Properties.RowType.IsEnum)
{
e.Row.Properties.ToolTip = ((Enum)(e.Row.Properties.Value)).GetDisplayText();
}
else if (e.Row.Properties.RowType.FullName == typeof(DateTime).FullName)
{
e.Row.Properties.ToolTip = ((DateTime)e.Row.Properties.Value).ToString("yyyy-MM-dd HH:mm:ss");
}
else if (e.Row.Properties.RowType.FullName == typeof(Image).FullName)
{
e.Row.Properties.ToolTip = string.Empty;
}
else
{
e.Row.Properties.ToolTip = e.Row.Properties.Value.ToString();
}
}
#endregion
#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 curvePro = (IsHydroCurveProAttribute)descriptor.Attributes[typeof(IsHydroCurveProAttribute)];
if (curvePro != null)
{
var buttonEdit = new RepositoryItemButtonEdit();
buttonEdit.TextEditStyle = TextEditStyles.DisableTextEditor;
buttonEdit.ButtonClick += delegate
{
var vm = GetPropertyViewModel(e.Row);
if (this.SelectCurveEvent == null || !this.SelectCurveEvent.Invoke(vm, curvePro.CurveType))
{
var hydroInfo = GetHydroInfoEvent?.Invoke();
if (hydroInfo == null)
{
return;
}
switch (curvePro.CurveType)
{
case HydroCurve.Pump:
{
}
break;
case HydroCurve.PumpQH:
{
var vmPump = vm as HydroPumpPropertyViewModel;
var curveQh = hydroInfo.Curves?.Find(x => x.Code == vmPump.CurveQH);
var curvePts = curveQh?.CurveData?.Select(x => new HydroCurvePointViewModel(x.X, x.Y)).ToList();
var dlg = new HydroCurveViewDlg();
dlg.Text = $"{vm.Name}-流量扬程曲线";
dlg.TitleTextX = "流量(m³/h)";
dlg.TitleTextY = "扬程(m)";
dlg.SetBindingData(curvePts);
dlg.ShowDialog();
}
break;
case HydroCurve.PumpQP:
{
}
break;
case HydroCurve.PumpQE:
{
}
break;
case HydroCurve.Valve:
{
}
break;
case HydroCurve.ValveQL:
{
}
break;
default: break;
}
}
};
e.RepositoryItem = buttonEdit;
}
#endregion
#region 模式
var patternPro = (IsHydroPatternProAttribute)descriptor.Attributes[typeof(IsHydroPatternProAttribute)];
if (patternPro != null)
{
var buttonEdit = new RepositoryItemButtonEdit();
buttonEdit.TextEditStyle = TextEditStyles.DisableTextEditor;
buttonEdit.ButtonClick += delegate
{
var vm = GetPropertyViewModel(e.Row);
this.SelectPatternEvent?.Invoke(vm, patternPro.PatternType);
};
e.RepositoryItem = buttonEdit;
}
#endregion
}
//属性值正在改变
void propertyGridControl1_CellValueChanging(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
{
var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last();
var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row);
var propertyViewModel = GetPropertyViewModel(e.Row);
this.PropertyValueChangingEvent?.Invoke(propertyViewModel);
}
//属性值改变
void propertyGridControl1_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
{
var fieldName = e.Row.Properties.FieldName.Split(new char[] { '.' }).Last();
var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row);
var propertyViewModel = GetPropertyViewModel(e.Row);
this.PropertyValueChangedEvent?.Invoke(propertyViewModel);
if (this.PropertyValueChangedEvent == null || !this.PropertyValueChangedEvent.Invoke(propertyViewModel))
{
var hydroInfo = GetHydroInfoEvent?.Invoke();
if (hydroInfo == null)
{
return;
}
var allParterList = hydroInfo.GetAllParters();
var parter = allParterList?.Find(x => x.ID == propertyViewModel.ID);
if (parter == null)
{
return;
}
parter.UpdateProperty(allParterList, propertyViewModel);
}
UpdateRows();
}
// 描述
private void barBtnHelp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
var visible = this.splitterItem1.Visibility == LayoutVisibility.Always ? false : true;
SetDescriptionVisible(visible);
}
//全部展开
private void barBtnExpandAll_ItemClick(object sender, ItemClickEventArgs e)
{
this.propertyGridControl1.ExpandAllRows();
}
//全部折叠
private void barBtnCollpseAll_ItemClick(object sender, ItemClickEventArgs e)
{
this.propertyGridControl1.CollapseAllRows();
}
}
}