using Autofac; using Bro.Common.Base; using Bro.Common.Factory; using Bro.Common.Helper; using Bro.Common.Interface; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Bro.Common.Model.Forms { public partial class FrmDeviceOpConfigEditor : Form { public FrmDeviceOpConfigEditor() { InitializeComponent(); } //private DeviceOpBind bind = null; public DeviceOpBind Bind { get; set; } IOperationConfig backOpConfig = new OperationConfigBase(); List deviceList = null; IDevice currentDevice = null; public FrmDeviceOpConfigEditor(DeviceOpBind bind) { InitializeComponent(); InitialDeviceCbo(); Bind = bind; if (!string.IsNullOrWhiteSpace(bind.Device)) { cboDevice.SelectedValue = bind.Device; currentDevice = deviceList.FirstOrDefault(u => u.Id == bind.Device); if (currentDevice != null) { var attr = currentDevice.GetType().GetCustomAttribute(); if (attr != null) { backOpConfig = ConfigFactory.GetOperationConfig(attr.TypeCode); } } } else { if (cboDevice.Items.Count > 0) cboDevice.SelectedIndex = 0; } cboDevice.SelectedIndexChanged += CboDevice_SelectedIndexChanged; if (bind.OpConfig == null) { bind.OpConfig = new OperationConfigBase(); if (currentDevice != null) { var attr = currentDevice.GetType().GetCustomAttribute(); if (attr != null) bind.OpConfig = ConfigFactory.GetOperationConfig(attr.TypeCode); } } backOpConfig.DataFrom(bind.OpConfig); propGrid.SelectedObject = bind.OpConfig; } private void CboDevice_SelectedIndexChanged(object sender, EventArgs e) { ChangeOpConfigByDevice(); } private void ChangeOpConfigByDevice() { var device = deviceList.FirstOrDefault(u => u.Id == cboDevice.SelectedValue.ToString()); if (device != null) { var attr = device.GetType().GetCustomAttribute(); if (attr != null) { propGrid.SelectedObject = ConfigFactory.GetOperationConfig(attr.TypeCode); } } } private void InitialDeviceCbo() { using (var scope = GlobalVar.Container.BeginLifetimeScope()) { deviceList = scope.Resolve>(); if (deviceList.Count > 0) { UIHelper.SetCombo(cboDevice, new List(deviceList), "Name", "Id"); } } } private void btnCancel_Click(object sender, EventArgs e) { Bind.OpConfig = backOpConfig; this.DialogResult = DialogResult.Cancel; } private void btnConfirm_Click(object sender, EventArgs e) { Bind.Device = cboDevice.SelectedValue.ToString(); Bind.OpConfig = propGrid.SelectedObject as IOperationConfig; this.DialogResult = DialogResult.OK; } private void btnReset_Click(object sender, EventArgs e) { ChangeOpConfigByDevice(); } } }