using HMI.Core.Util;
|
using HMI.UI.DpUtil;
|
using HMI.UI.Util;
|
using Panuon.WPF.UI;
|
using Plugin.DOControl.Entity;
|
using System;
|
using System.Collections.ObjectModel;
|
using System.ComponentModel;
|
using System.Linq;
|
|
namespace Plugin.DOControl
|
{
|
/// <summary>
|
/// ModalWindow.xaml 的交互逻辑
|
/// </summary>
|
public partial class DataModal : WindowX
|
{
|
public DataModal()
|
{
|
this.DataContext = this;
|
InitializeComponent();
|
}
|
|
public DOControlData Data { get; set; }
|
|
private ObservableCollection<DOControlData> datasource;
|
|
public ObservableCollection<DOControlData> Datasource
|
{
|
get { return datasource; }
|
set { Set(ref datasource, value); }
|
}
|
|
private bool _isProcessing;
|
|
protected override void OnClosing(CancelEventArgs e)
|
{
|
//Press Continue Button
|
if (DialogResult == true)
|
{
|
if (_isProcessing)
|
{
|
e.Cancel = true;
|
return;
|
}
|
var okButton = WindowXModalDialog.GetOKButton(this);
|
try
|
{
|
_isProcessing = true;
|
ButtonHelper.SetIsPending(okButton, true);
|
if (!ValidateForm())
|
{
|
e.Cancel = true;
|
return;
|
}
|
|
SaveOrUpdateData();
|
}
|
catch (Exception ex)
|
{
|
ToastHelper.Error(this, $"Error!{ex.Message}");
|
e.Cancel = true;
|
return;
|
}
|
finally
|
{
|
_isProcessing = false;
|
ButtonHelper.SetIsPending(okButton, false);
|
}
|
}
|
base.OnClosing(e);
|
}
|
|
private bool ValidateName(object value, out string err)
|
{
|
err = null;
|
//string group = TbGroup.Text;
|
//var temp = Datasource.FirstOrDefault(d => d.Group == group && d.Name == value.ToString());
|
//if (temp != null && temp != Data)
|
//{
|
// err = this.lang("App.MustBeUnique", $"{this.lang("doc.group")},{this.lang("doc.name")}");
|
// return false;
|
//}
|
return true;
|
}
|
|
private bool ValidateAddress(object value, out string err)
|
{
|
err = null;
|
//string group = TbGroup.Text;
|
//var temp = Datasource.FirstOrDefault(d => d.Address == value.ToString());
|
//if (temp != null && temp != Data)
|
//{
|
// err = this.lang("App.MustBeUnique", this.lang("doc.Address"));
|
// return false;
|
//}
|
return true;
|
}
|
|
private bool ValidateForm()
|
{
|
bool flag = FormGroupHelper.ValidateForm(formPanel);
|
if (flag)
|
{
|
Data.Group = TbGroup.Text;
|
Data.Name = TbName.Text;
|
Data.Address = TbAddress.Text;
|
Data.ReadOnly = CxReadOnly.IsChecked == true;
|
Data.PressDownValue = TbPressDownValue.Text;
|
Data.PressUpValue = TbPressUpValue.Text;
|
Data.ControlType = (ControlType)CbControlType.SelectedItem;
|
}
|
return flag;
|
}
|
|
public bool? Open(DOControlData data, ObservableCollection<DOControlData> datasource)
|
{
|
Datasource = datasource;
|
Data = data;
|
|
TbGroup.Text = Data.Group;
|
TbName.Text = Data.Name;
|
TbAddress.Text = Data.Address;
|
CxReadOnly.IsChecked = Data.ReadOnly;
|
TbPressDownValue.Text = Data.PressDownValue;
|
TbPressUpValue.Text = Data.PressUpValue;
|
CbControlType.SelectedItem = Data.ControlType;
|
return this.ShowDialog();
|
}
|
|
private void SaveOrUpdateData()
|
{
|
int index = -1;
|
if (Datasource != null)
|
{
|
index = Datasource.IndexOf(Data);
|
}
|
if (index < 0)
|
{
|
InsertData(Data);
|
}
|
else
|
{
|
UpdateData(index, Data);
|
}
|
}
|
|
public void InsertData(DOControlData data)
|
{
|
Datasource.Add(data);
|
XmlHelper.SerializeToXml(DOControl.FilePath, Datasource);
|
}
|
|
public void UpdateData(int index, DOControlData data)
|
{
|
var temp = XmlHelper.DeepClone(Datasource);
|
temp[index] = data;
|
XmlHelper.SerializeToXml(DOControl.FilePath, temp);
|
}
|
}
|
}
|