using DevExpress.XtraEditors.Repository; using DevExpress.XtraVerticalGrid.Events; using System.Data; namespace HydroUI { public partial class PropertyForm : UserControl { public PropertyForm() { InitializeComponent(); this.propertyGridControl1.OptionsBehavior.PropertySort = DevExpress.XtraVerticalGrid.PropertySort.NoSort; this.propertyGridControl1.OptionsView.AllowReadOnlyRowAppearance = DevExpress.Utils.DefaultBoolean.True; this.propertyGridControl1.OptionsView.ShowFocusedFrame = false; this.propertyGridControl1.OptionsView.ShowRootLevelIndent = false; this.propertyGridControl1.RecordWidth = 120; this.propertyGridControl1.RowHeaderWidth = 100; propertyGridControl1.CustomRecordCellEdit += propertyGridControl1_CustomRecordCellEdit; propertyGridControl1.CustomDrawRowHeaderCell += propertyGridControl1_CustomDrawRowHeaderCell; propertyGridControl1.CustomDrawRowValueCell += propertyGridControl1_CustomDrawRowValueCell; propertyGridControl1.ShowingEditor += propertyGridControl1_ShowingEditor; this.comboBox_type.Properties.TextEditStyle = TextEditStyles.DisableTextEditor; } private IBaseViewModel _selectedObject; private MapObjectType _selectedType = MapObjectType.全部; private List selectionSets; public SelectionSet selectionSet = new SelectionSet(); private Form fr = null; public void SetEnabled(bool isReadOnly) { } public void SetWindows(Form mg) { if (mg.WindowState == FormWindowState.Maximized) { this.Height = mg.Height - 50; this.Location = new Point(mg.Left + mg.Width - this.Width, mg.Top + 50); } else { this.Height = mg.Height; this.Location = new Point(mg.Left + mg.Width - 15, mg.Top); } fr = mg; } public Form GetWindow() { return this.fr; } public void SetObj(object o) { propertyGridControl1.SelectedObject = o; this.Text = o.GetType().Name; } public void SetObj(IBaseViewModel selectedObject) { lock (selectedObject) { _selectedObject = selectedObject; if (_selectedObject is JunctionViewModel junc) { _selectedType = MapObjectType.节点; propertyGridControl1.SelectedObjects = new JunctionViewModel[] { junc }; } else if (_selectedObject is ReservoirViewModel res) { _selectedType = MapObjectType.水库; propertyGridControl1.SelectedObjects = new ReservoirViewModel[] { res }; } else if (_selectedObject is TankViewModel tank) { _selectedType = MapObjectType.水池; propertyGridControl1.SelectedObjects = new TankViewModel[] { tank }; } else if (_selectedObject is PipeViewModel pipe) { _selectedType = MapObjectType.管线; propertyGridControl1.SelectedObjects = new PipeViewModel[] { pipe }; } else if (_selectedObject is ValveViewModel valve) { _selectedType = MapObjectType.阀门; propertyGridControl1.SelectedObjects = new ValveViewModel[] { valve }; } else if (_selectedObject is RepeaterViewModel repeater) { _selectedType = MapObjectType.重复器; propertyGridControl1.SelectedObjects = new RepeaterViewModel[] { repeater }; } //水表 else if (_selectedObject is MeterViewModel meter) { _selectedType = MapObjectType.水表; propertyGridControl1.SelectedObjects = new MeterViewModel[] { meter }; } //水泵 else if (_selectedObject is PumpViewModel pump) { _selectedType = MapObjectType.水泵; propertyGridControl1.SelectedObjects = new PumpViewModel[] { pump }; } //喷头Nozzle else if (_selectedObject is NozzleViewModel nozzle) { _selectedType = MapObjectType.喷头; propertyGridControl1.SelectedObjects = new NozzleViewModel[] { nozzle }; } else { _selectedType = MapObjectType.全部; propertyGridControl1.SelectedObjects = new IBaseViewModel[] { selectedObject }; } switch (_selectedType) { case MapObjectType.节点: Text = "节点 Properties"; break; case MapObjectType.水库: Text = "水库 Properties"; break; case MapObjectType.水池: Text = "水池 Properties"; break; case MapObjectType.管线: Text = "管线 Properties"; break; case MapObjectType.阀门: Text = "阀门 Properties"; break; case MapObjectType.重复器: Text = "重复器 Properties"; break; } } } public void SetNet(MapViewNetWork net) { selectionSet.net = net; RefreshListBox(); } public void SetObjs(List selectedObjects) { if (selectedObjects == null) return; selectedObjects.RemoveAll(obj => obj == null); selectedObjects.ForEach(obj => { if (obj.Tags == null) { obj.Tags = new TagList(); } }); selectionSet.selectedObjects = selectedObjects.ToList(); if (selectedObjects.Count == 1) SetObj(selectedObjects[0]); else ShowProperties(selectedObjects); if (selectionSet.OnlyViewSelected) RefreshListBox(); } private void PropertyForm_Load_1(object sender, EventArgs e) { foreach (var item in Enum.GetValues(typeof(MapObjectType))) { comboBox_type.Properties.Items.Add(item); } comboBox_type.SelectedIndex = 0; selectionSets = new List(); selectionSet = new SelectionSet(); this.listBoxControl1.DataSource = selectionSet.View; } private void comboBox_type_SelectedIndexChanged(object sender, EventArgs e) { RefreshListBox(); } void RefreshListBox() { if (selectionSet == null) return; if (comboBox_type.Properties.Items.Count < 1) { foreach (var item in Enum.GetValues(typeof(MapObjectType))) { comboBox_type.Properties.Items.Add(item); } comboBox_type.SelectedIndex = 0; } string txt = comboBox_type.SelectedItem.ToString(); MapObjectType selectedType = (MapObjectType)Enum.Parse(typeof(MapObjectType), txt); // 使用 LINQ 查询筛选选择集 selectionSet.filterType = selectedType; selectionSet.FilterString = textBox_search.Text; this.listBoxControl1.DataSource = null; this.listBoxControl1.DataSource = selectionSet.View; } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (this.listBoxControl1.SelectedItems.Count == 0) { return; } else if (this.listBoxControl1.SelectedItems.Count == 1) { var objID = this.listBoxControl1.SelectedItems[0]; var findlist = selectionSet.net.MapObjects.FindAll(o => o.ID == objID); if (findlist.Count <= 0) return; IBaseViewModel mapObject = findlist[0];// selectionSet.selectedObjects.Find(obj => obj.ID == ((MapObject)this.listBoxControl1.SelectedItems[0]).ID); if (mapObject != null) SetObj(mapObject); GlobalObject.map.selectedObjs.ForEach(o => o.Selected = false); ; GlobalObject.map.selectedObjs.Clear(); GlobalObject.map.selectedObjs.Add(mapObject); mapObject.Selected = true; GlobalObject.map.SetMapInvalidate(); } else { List list = new List(); foreach (var o in this.listBoxControl1.SelectedItems) { var objID = this.listBoxControl1.SelectedItems[0]; IBaseViewModel mapObject = selectionSet.net.MapObjects.FindAll(obj => obj.ID == objID)[0]; list.Add(mapObject); } ShowProperties(list); GlobalObject.map.selectedObjs.ForEach(o => o.Selected = false); ; GlobalObject.map.selectedObjs.Clear(); list.ForEach(o => o.Selected = true); GlobalObject.map.selectedObjs.AddRange(list); GlobalObject.map.SetMapInvalidate(); } } private void listBox1_DoubleClick(object sender, EventArgs e) { if (this.listBoxControl1.SelectedItems.Count == 1) { var objID = this.listBoxControl1.SelectedItems[0]; IBaseViewModel mapObject = selectionSet.net.MapObjects.FindAll(obj => obj.ID == objID)[0]; GlobalObject.map.setCenter(mapObject); GlobalObject.map.SetMapInvalidate(); } } public void ShowProperties(IEnumerable selectedObjects) { IBaseViewModel[] objs = null; try { if (selectedObjects != null) objs = selectedObjects.ToArray(); } catch { return; } propertyGridControl1.SelectedObjects = objs; } private void textBox_search_TextChanged(object sender, EventArgs e) { RefreshListBox(); } private void checkBox_OnlyShowSelected_CheckedChanged(object sender, EventArgs e) { selectionSet.OnlyViewSelected = checkBox_OnlyShowSelected.Checked; RefreshListBox(); } private void label_lock_Click(object sender, EventArgs e) { var label_lock = (Label)sender; GlobalObject.LockSelect = !GlobalObject.LockSelect; if (GlobalObject.LockSelect) { label_lock.BackColor = Color.Blue; label_lock.Text = "🔒"; } else { label_lock.BackColor = Color.Green; label_lock.Text = "🔓"; } } private void label_filter_Click(object sender, EventArgs e) { var label_lock = (Label)sender; TagList tags = new TagList(); var mobjs = selectionSet.OnlyViewSelected ? selectionSet.selectedObjects : selectionSet.net.MapObjects; foreach (var item in mobjs) { tags.AddRange(item.Tags); } var t = tags.Distinct().ToList(); tags.Clear(); tags.AddRange(t); var form = new FormFilter(tags); if (form.ShowDialog() == DialogResult.OK) { selectionSet.FilterTags = form.Tags; selectionSet.rules = form.rules; GlobalObject.ApplyFilter = selectionSet.FilterTags.Count > 0 || selectionSet.rules.Count > 0; if (GlobalObject.ApplyFilter) { label_lock.BackColor = Color.Blue; label_lock.Text = "⏬"; } else { label_lock.BackColor = Color.Green; label_lock.Text = "🔽"; } RefreshListBox(); } } private void PropertyForm_KeyDown(object sender, KeyEventArgs e) { //Ctrl+A,则选中当前listbox中的所有项 if (e.Control && e.KeyCode == Keys.A) { for (int i = 0; i < this.listBoxControl1.Items.Count; i++) { this.listBoxControl1.SetSelected(i, true); } } } private void listBoxControl1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { this.popupMenuRight.ShowPopup(MousePosition); } } private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (this.listBoxControl1.SelectedItems.Count == 1) { GlobalObject.map.DeleteChoosedObj(); RefreshListBox(); } } #region Property //自定义属性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 realFieldName = fieldName; //属性描述器 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; } } } //自定义属性值显示 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 if (fullTypeName == typeof(string[]).FullName) { var stringValue = (string[])e.Properties.Value; e.CellText = stringValue?.Length.ToString(); } else if (fullTypeName == typeof(DictionaryPropertyAdapter).FullName) { e.CellText = string.Empty; } else { var descriptor = this.propertyGridControl1.GetPropertyDescriptor(e.Row); if (descriptor != null) { 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; #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 富文本 if (descriptor != null) { 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 } #endregion } public class SelectionSet { public string Name; public MapObjectType filterType=MapObjectType.全部; public string FilterString { get; set; } = ""; public TagList FilterTags { get; set; } = new TagList(); public List> rules = new List>(); public List FilterTypes = new List(); public MapViewNetWork net { get; set; } = null; public bool OnlyViewSelected { get; set; } = true; public List selectedObjects=new List(); public List CurrentSelectedObjects=new List(); public List View { get { List originList; List list; List list_result=new List(); if (!OnlyViewSelected && net != null) { originList = net.MapObjects; } else { originList = selectedObjects; } if (filterType == MapObjectType.全部) { list = originList; } else { var filteredSets = from obj in originList where obj.GetType() == filterType.GetObjType() select obj; list = filteredSets?.ToList(); } if (FilterString!=null && FilterString!="") { list = list?.FindAll(o => o.ID.IndexOf(FilterString) >= 0); } if (FilterTags.Count>0)//使用表达式,对结果进行筛选 { foreach (var tag in FilterTags) { var filteredSets = from obj in list where obj.Tags.Contains(tag) select obj; list = filteredSets?.ToList(); } } if(rules.Count>0) { foreach (var rule in rules) { var filteredSets = from obj in list where rule(obj) select obj; list = filteredSets?.ToList(); } } CurrentSelectedObjects = list; list_result = list.Select(o => o.ID).ToList(); return list_result; } } } }