using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Drawing.Design; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static Hydro.MapView.MapViewEnum; namespace Hydro.MapView { public interface IBaseViewModel { [Category("基本信息")] [Description("对象的ID唯一标识")] [DisplayName("编号")] string ID { get; set; } [Category("其他参数")] [Description("选中")] [DisplayName("选中")] [Browsable(false)] bool Selected { get; set; } [Browsable(false)] bool Hovered { get; set; } [Category("其他参数")] [Description("选中")] [DisplayName("位置信息")] [Browsable(false)] PointF Position { get; set; }//= new PointF(0, 0); [Browsable(false)] [JsonIgnore] String regionName { get; set; }//= null; [Category("基本信息")] [Description("X坐标")] [DisplayName("X坐标")] [Browsable(true)] float X { get; set; } //{ // get // { // return Position.X; // } // set // { // Position = new PointF(value, Position.Y); // } //} [Category("基本信息")] [Description("Y坐标")] [DisplayName("Y坐标")] [Browsable(true)] float Y { get; set; } //{ // get // { // return Position.Y; // } // set // { // Position = new PointF(Position.X, value); // } //} [Category("基本信息")] [Description("标高")] [DisplayName("标高")] [Browsable(true)] float Elev { get; set; } [Category("其他参数")] [Description("对象的等级")] [DisplayName("级别")] //[Editor(typeof(MyPropertyEditor), typeof(UITypeEditor))] int Level { get; set; } //= 0; [Category("其他参数")] [Description("对象的等级")] [DisplayName("是否显示")] bool Visible { get; set; } //= true; //[Category("4、其他参数")] //[Description("标签集合")] //[DisplayName("标签")] //public string Tags { get; set; } = null; [Category("基本信息")] [Description("类型")] [DisplayName("类型")] MapObjectType Type { get; }// { get { return this.GetTypeString(); } } //[Browsable(false)] [Category("其他参数")] [Description("ID类型")] [DisplayName("ID类型")] [Browsable(false)] string IDType { get; }// => Type.ToString()+"\t"+ ID; MapObjectType GetTypeString(); TagList Tags { get; set; } //string TagsString { get; set; } bool isNode(); } [Serializable] public class TagList:List { [Category("其他参数")] [Browsable(false)] public override string ToString() { if (Count==0) return "null"; else return string.Join(",", this); } //public TagList(string value):base(value.Split(',')) //{ // if (string.IsNullOrEmpty(value) || value == "null") // { // base.Clear(); // return; // } //} public TagList() { } public TagList(string value) { if (string.IsNullOrEmpty(value) || value == "null") { base.Clear(); return; } string[] tags = value.Split(','); base.AddRange(tags); } } public class MyEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { List tags = value as List; // Create and show dialog TagEditorForm form = new TagEditorForm(tags); //form弹出时,位置在鼠标位置 form.StartPosition = FormStartPosition.Manual; form.Location =new Point( Cursor.Position.X-form.Width+10,Cursor.Position.Y-10); //if (form.ShowDialog() == DialogResult.OK) //{ //} form.ShowDialog(); return form.Tags; //return base.EditValue(context, provider, value); } } public class TagEditorForm : Form { public List Tags { get; set; } private ListBox listBox; private TextBox tb_text; public TagEditorForm(List tags) { Tags = tags; // Initialize ListBox listBox = new ListBox(); listBox.DataSource = Tags; listBox.Dock = DockStyle.Fill; this.Controls.Add(listBox); // Add buttons for add, remove, update operations tb_text = new TextBox(); tb_text.Dock = DockStyle.Top; this.Controls.Add(tb_text); Button addButton = new Button(); addButton.Dock = DockStyle.Bottom; addButton.Text = "添加"; addButton.Click += AddButton_Click; this.Controls.Add(addButton); Button removeButton = new Button(); removeButton.Dock = DockStyle.Bottom; removeButton.Text = "删除"; removeButton.Click += RemoveButton_Click; this.Controls.Add(removeButton); Button updateButton = new Button(); updateButton.Dock = DockStyle.Bottom; updateButton.Text = "更新"; updateButton.Click += UpdateButton_Click; this.Controls.Add(updateButton); Panel panel = new Panel(); updateButton.Dock = DockStyle.Bottom; this.Controls.Add(panel); Button btn_ok = new Button(); btn_ok.Dock=DockStyle.Left; btn_ok.Click+=(o,e) => { this.DialogResult = DialogResult.OK; }; btn_ok.Text = "确定"; panel.Controls.Add(btn_ok); Button btn_cancel = new Button(); btn_cancel.Dock = DockStyle.Right; btn_ok.Click += (o, e) => { this.DialogResult = DialogResult.Cancel; }; btn_cancel.Text = "取消"; panel.Controls.Add(btn_cancel); listBox.SelectedIndexChanged += (o,e) => { if (listBox.SelectedItem!=null) tb_text.Text= listBox.SelectedItem.ToString(); }; this.Icon =Icon.ExtractAssociatedIcon(Application.ExecutablePath); } private void AddButton_Click(object sender, EventArgs e) { // 在listBox中按tb_text的内容,添加新的tag if (tb_text.Text != null && tb_text.Text != "") { if (Tags == null) Tags = new List(); Tags.Add(tb_text.Text); listBox.DataSource = null; listBox.DataSource = Tags; } } private void RemoveButton_Click(object sender, EventArgs e) { //删除listBox中选中的tag if (listBox.SelectedItem != null) { Tags.Remove(listBox.SelectedItem.ToString()); listBox.DataSource = null; listBox.DataSource = Tags; } // Remove tag } private void UpdateButton_Click(object sender, EventArgs e) { // 在listBox中按tb_text的内容,更新tag,但是不改变顺序 if (listBox.SelectedItem != null) { Tags[listBox.SelectedIndex] = tb_text.Text; listBox.DataSource = null; listBox.DataSource = Tags; } } } }