wenxiangchao
3 天以前 b544d5cccd76735e034c8055918a50927b437474
HMI.ParameterManage/SettingModal.xaml.cs
@@ -3,6 +3,8 @@
using HMI.UI.DpUtil;
using HMI.UI.Util;
using Microsoft.Win32;
using NPOI.SS.Formula.Functions;
using NPOI.SS.Formula.PTG;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
@@ -11,9 +13,54 @@
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Interop;
namespace HMI.ParameterManage
{
    public class BoolData
    {
        public int Index { get; }
        public bool Value { get; set; }
        public BoolData(int index)
        {
            this.Index = index;
        }
    }
    public class IntData
    {
        public int Index { get; }
        public int Value { get; set; }
        public IntData(int index)
        {
            this.Index = index;
        }
    }
    public class DoubleData
    {
        public int Index { get; }
        public double Value { get; set; }
        public DoubleData(int index)
        {
            this.Index = index;
        }
    }
    public class StringData
    {
        public int Index { get; }
        public string Value { get; set; } = string.Empty;
        public StringData(int index)
        {
            this.Index = index;
        }
    }
    /// <summary>
    /// ModalWindow.xaml 的交互逻辑
    /// </summary>
@@ -71,6 +118,14 @@
        public bool ValidateName(object value, out string err)
        {
            err = null;
            string pattern = @"^[\p{L}_][\p{L}\p{N}_]*$";
            if (!Regex.IsMatch(value.ToString(), pattern))
            {
                err = "参数名不能以数字开头";
                return false;
            }
            var data = Datasource?.FirstOrDefault(e => e.Name == value.ToString());
            if (data != null && data != Data)
            {
@@ -93,6 +148,9 @@
            {
                Data.Name = TbName.Text;
                Data.Type = CbType.Text;
                Data.IsArray = CbIsArray.IsChecked == true;
                Data.Length = int.Parse(NiLength.Text);
                Data.Value = GetTargetValue();
                Data.Unit = TbUnit.Text;
                Data.Description = TbDescription.Text;
@@ -102,14 +160,23 @@
            return flag;
        }
        public bool? Open(ParameterData data, ObservableCollection<ParameterData> datasource,List<string> groups)
        public bool? Open(ParameterData data, ObservableCollection<ParameterData> datasource, List<string> groups,string productName = null)
        {
            if (!string.IsNullOrEmpty(productName))
            {
                FgProductName.Visibility = System.Windows.Visibility.Visible;
                TbProductName.Text = productName;
            }
            Datasource = datasource;
            Data = data;
            TbGroup.ItemsSource = groups;
            TbName.Text = Data.Name;
            CbType.SelectedValue = Data.Type;
            CbIsArray.IsChecked = Data.IsArray;
            NiLength.Text = Data.Length.ToString();
            UpdateWindow(Data.Type);
            UpdateForm(Data);
@@ -122,11 +189,18 @@
        private string GetTargetValue()
        {
            if (CbType.Text == "Int" || Data.Type == "Double")
            var type = CbType.Text;
            if (CbIsArray.IsChecked == true)
            {
                var arr = (object[])arrayInput.ItemsSource;
                return string.Join(",", arr.Select(e => e.GetType().GetProperty("Value").GetValue(e).ToString()).ToArray());
            }
            if (type == "Int" || type == "Double")
            {
                return NiNumberValue.Text;
            }
            else if (CbType.Text == "Bool")
            else if (type == "Bool")
            {
                return CbBoolValue.Text;
            }
@@ -138,17 +212,25 @@
        private void UpdateForm(ParameterData data)
        {
            if (Data.Type == "Int" || Data.Type == "Int")
            if (data.IsArray)
            {
                NiNumberValue.Text = Data.Value;
            }
            else if (Data.Type == "Bool")
            {
                CbBoolValue.SelectedValue = Data.Value;
                string[] arr = data.Value.Split(',');
                arrayInput.ItemsSource = RefreshArray(arr, Data.Type);
            }
            else
            {
                TbStringValue.Text = Data.Value;
                if (Data.Type == "Int" || Data.Type == "Int")
                {
                    NiNumberValue.Text = Data.Value;
                }
                else if (Data.Type == "Bool")
                {
                    CbBoolValue.SelectedValue = Data.Value;
                }
                else
                {
                    TbStringValue.Text = Data.Value;
                }
            }
        }
@@ -182,6 +264,10 @@
                val = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
            }
            UpdateWindow(val);
            if (CbIsArray.IsChecked == true)
            {
                arrayInput.ItemsSource = RefreshArray(GetLength(), val);
            }
        }
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
@@ -192,5 +278,111 @@
                TbStringValue.Text = ofd.FileName;
            }
        }
        private void CbIsArray_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            bool isChecked = ((CheckBox)sender).IsChecked == true;
            if (isChecked)
            {
                FgLength.Visibility = System.Windows.Visibility.Visible;
                arrayInput.Visibility = System.Windows.Visibility.Visible;
                arrayInput.ItemsSource = RefreshArray(GetLength(), CbType.Text);
            }
            else
            {
                FgLength.Visibility = System.Windows.Visibility.Collapsed;
                arrayInput.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        private object[] RefreshArray(int length, string type)
        {
            var arr = new object[length];
            for (int i = 0; i < length; i++)
            {
                if (type == "Int")
                {
                    arr[i] = new IntData(i);
                }
                else if (type == "Double")
                {
                    arr[i] = new DoubleData(i);
                }
                else if (type == "Bool")
                {
                    arr[i] = new BoolData(i);
                }
                else
                {
                    arr[i] = new StringData(i);
                }
            }
            return arr;
        }
        private object[] RefreshArray(string[] source, string type)
        {
            var arr = new object[source.Length];
            type = string.IsNullOrEmpty(type) ? "String" : type;
            for (int i = 0; i < arr.Length; i++)
            {
                if (type == "Int")
                {
                    arr[i] = new IntData(i)
                    {
                        Value = int.Parse(source[i])
                    };
                }
                else if (type == "Double")
                {
                    arr[i] = new DoubleData(i)
                    {
                        Value = double.Parse(source[i])
                    };
                }
                else if (type == "Bool")
                {
                    arr[i] = new BoolData(i)
                    {
                        Value = bool.Parse(source[i])
                    };
                }
                else
                {
                    arr[i] = new StringData(i)
                    {
                        Value = source[i]
                    };
                }
            }
            return arr;
        }
        private int GetLength()
        {
            try
            {
                string text = NiLength.Text;
                if (text.Contains("."))
                {
                    text = text.Split('.')[0];
                }
                int length = int.Parse(text);
                return length > 0 ? length : 1;
            }
            catch
            {
                return 1;
            }
        }
        private void NiLength_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (CbIsArray.IsChecked != true)
            {
                return;
            }
            arrayInput.ItemsSource = RefreshArray(GetLength(), CbType.Text);
        }
    }
}