tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; 
 
namespace TProduct.WinFrmUI.Data4Factory
{
    public partial class SelectProductTypeComboxContentCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        public SelectProductTypeComboxContentCtrl()
        {
            InitializeComponent();
            this.gridView1.SetNormalEditView();
            this.layoutControl1.SetupLayoutControl();
 
        }
        private class CurrentViewModel : Model.ProductStyle
        {
            public CurrentViewModel() { }
            public CurrentViewModel(Model.ProductStyle rhs) : base(rhs)
            {
 
            }
 
            public bool IsChecked { get; set; }
        }
 
        public event Action<Model.ProductStyle> FocusedDataChangedEvent = null;
        public event Action<Model.ProductStyle> AddDataChanedEvent = null;
        public event Action<Model.ProductStyle> EditDataChanedEvent = null;
 
        private List<CurrentViewModel> _bindList = null;
        public Model.eProductType _etype;
        public void SetBindingData(Model.eProductType type) 
        {
            _etype = type;
            var all = new BLL.ProductStyle().GetAll()?.Where(x=>x.Type== type)?.ToList();
            _bindList = new List<CurrentViewModel>();
            all?.ForEach(x =>_bindList.Add(new CurrentViewModel(x)));
            this.bindingSource1.DataSource = _bindList;
            this.bindingSource1.ResetBindings(false);
        }
 
 
        /// <summary>
        /// 初始化并设置默认值
        /// </summary>
        public bool SetFocused(Model.eProductType type,long ID)
        {
            this.SetBindingData(type);
            if (ID<1)
                return false;
            var selModel = _bindList.Find(x => x.ID == ID);
            if (selModel != null)
            {
                selModel.IsChecked = true;
                this.bindingSource1.ResetBindings(false);
                this.FocusedDataChangedEvent?.Invoke(selModel);
            }
            return true;
        }
 
        /// <summary>
        /// 获取聚焦
        /// </summary>
        public object GetFocused()
        {
            var vm = this.gridView1.GetFocusedRow() as CurrentViewModel;
            return vm;
        }
 
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            var row = this.gridView1.GetFocusedRow() as CurrentViewModel;
            if (row == null)
                return;
            this.FocusedDataChangedEvent?.Invoke(row);
        }
 
        private void barButAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            WaitFrmHelper.ShowWaitForm();
            var dlg = new AddProductStyleDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(_etype);
            dlg.ReloadDataEvent += (rhs) =>
            {
                var vm = new CurrentViewModel(rhs);
                _bindList.ForEach(x => x.IsChecked = false);
                vm.IsChecked = true;
                _bindList.Add(vm);
                this.bindingSource1.ResetBindings(false);
                this.AddDataChanedEvent?.Invoke(rhs as Model.ProductStyle);
                this.FocusedDataChangedEvent?.Invoke(rhs as Model.ProductStyle);
                XtraMessageBox.Show("添加成功!");
            };
            dlg.ShowDialog();
        }
 
        private void barButEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            WaitFrmHelper.ShowWaitForm();
            var row = this.gridView1.GetCurrentViewModel(_bindList);
            if (row == null)
            {
                WaitFrmHelper.ShowWaitForm();
                XtraMessageBox.Show("请选择一行数据!");
                return;
            }
            var dlg = new EditProductStyleDlg();
            dlg.Shown += delegate { WaitFrmHelper.HideWaitForm(); };
            dlg.SetBindingData(row);
            dlg.ReloadDataEvent += (rhs) =>
            {
                row.Reset(rhs);
                this.bindingSource1.ResetBindings(false);
                EditDataChanedEvent?.Invoke(rhs as Model.ProductStyle);
                XtraMessageBox.Show("修改成功!");
            };
            dlg.ShowDialog();
        }
 
        private void barButSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.barButSearch1.Visibility == DevExpress.XtraBars.BarItemVisibility.Always)
                this.barButSearch1.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            else
                this.barButSearch1.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
        }
    }
}