qin
2024-06-04 77ab118a04eb43da0ce07b2f22d4a4bc82f04a7f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
 
namespace Hydro.MapView
{
    public class DlTemplateEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
 
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider serviceProvider, object value)
        {
            if (context != null && serviceProvider != null)
            {
                IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)serviceProvider.GetService(typeof(IWindowsFormsEditorService));
 
                if (editorService != null)
                {
                    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Data\\WaterEquivalent.Json");
                    List<string> wds = new List<string>();
                    if (File.Exists(filePath))
                    {
                        var json = File.ReadAllText(filePath);
                        if (!string.IsNullOrEmpty(json))
                        {
                            var w = JsonConvert.DeserializeObject<List<EquivalentTemplateModel>>(json);
                            if (w != null)
                            {
                                foreach (var item in w)
                                {
                                    wds.Add(item.Name);
                                }
                            }
                        }
                    }
                    // 创建下拉列表
                    ComboBox comboBox = new ComboBox();
                    comboBox.Items.AddRange(wds.ToArray());
 
                    // 显示下拉列表
                    editorService.DropDownControl(comboBox);
 
                    // 返回选中的值
                    value = comboBox.SelectedItem;
                }
            }
 
            return value;
        }
    }
}