duheng
2024-06-07 946d3d3c9701fd32bd3dd4b384c5a3684e59da24
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
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
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 static ImageComboBoxEdit ImageComboBoxEdit = null;
 
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider serviceProvider, object value)
        {
            ImageComboBoxEdit = new ImageComboBoxEdit();
            if (context != null && serviceProvider != null)
            {
                IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)serviceProvider.GetService(typeof(IWindowsFormsEditorService));
 
                if (editorService != null)
                {
                    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Data\\WaterEquivalent.Json");
                    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)
                                {
                                    var imageItem = new ImageComboBoxItem(item.Name, item.ID);
                                    ImageComboBoxEdit.Properties.Items.Add(imageItem);
                                }
                            }
                            editorService.DropDownControl(ImageComboBoxEdit);
                        }
                    }
                    value = ImageComboBoxEdit.Text;
                }
            }
            return value;
        }
    }
}