| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using Aitex.Core.RT.Log;using System.Xml;using Aitex.Core.UI.ControlDataContext;using System.Collections.ObjectModel;namespace Aitex.Core.UI.Control{    /// <summary>    /// Interaction logic for MOSource.xaml    /// </summary>    public partial class MOSource : UserControl    {        string _moConfig;        public MOSource()        {            InitializeComponent();        }        public static readonly DependencyProperty MOSourceDataListProperty = DependencyProperty.Register(                        "MOSourceDataList", typeof(ObservableCollection<MOSourceDataItem>), typeof(MOSource),                        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));        public ObservableCollection<MOSourceDataItem> MOSourceDataList        {            get            {                return (ObservableCollection<MOSourceDataItem>)this.GetValue(MOSourceDataListProperty);            }            set            {                this.SetValue(MOSourceDataListProperty, value);            }        }        protected override void OnRender(DrawingContext drawingContext)        {            base.OnRender(drawingContext);            UpdateMOLayout(_moConfig);        }        public void SetMOLayout(string moConfig)        {            _moConfig = moConfig;        }        void UpdateMOLayout(string moConfig)        {            double left = 0;            double width = this.Width;            double intervalWidth = 0;            int splitBath = -1;            double bathWidth = 0;            int objIndex = 0;            UserControl bathUc;            double bathLeft = -1;            int bathIntervalCount = 0;            ObservableCollection<UILayoutDataItem> obj = new ObservableCollection<UILayoutDataItem>();            if (rootCanvas.Children.Count > 0)            {                return;            }            if (string.IsNullOrEmpty(moConfig))            {                //LOG.Error("MO源配置文件为空");                return;            }            //index, name, type            List<Tuple<int, string, string,int>> items = new List<Tuple<int, string, string,int>>();            XmlDocument doc = new XmlDocument();            doc.LoadXml(moConfig);            XmlNodeList lstNode = doc.SelectNodes("MOSourceConfig/MOSources/MOSourceItem");            foreach (XmlNode node in lstNode)            {                try                {                    int index = int.Parse(node.Attributes["DisplayIndex"].Value) - 1;                    string name = node.Attributes["Name"].Value;                    string type = node.Attributes["Type"].Value;                    int bathindex = int.Parse(node.Attributes["BathIndex"].Value) - 1;                    if (!string.IsNullOrEmpty(name))                    {                        items.Add(new Tuple<int, string, string,int>(index, name, type,bathindex));                    }                }                catch (Exception ex)                {                    LOG.WriteExeption("MO源配置项错误," + node.Name, ex);                }            }            items.Sort((p1, p2) => p1.Item1.CompareTo(p2.Item1));            foreach (var item in items)            {                UserControl uc;                switch (item.Item3)                {                    case "WithDilute": uc = new MODilute(); break;                    case "WithPush": uc = new MOPush(); break;                    case "General": uc = new MOGeneral();break;                    default:                        uc = new MOGeneral(); break;                }                obj.Add(new UILayoutDataItem());                obj[objIndex].uc = uc;                obj[objIndex].index = item.Item1;                                if (splitBath == -1 && item.Item4!=-1) //第一个Bath,如果bathIndex为-1,表示没有水槽                {                    bathWidth = uc.Width;                    obj[objIndex].bathWidth = bathWidth;                }                else if ((item.Item4 == splitBath) && (item.Item4 != -1)) //一个Bath对应多个MO源                {                    bathWidth += uc.Width;                    if (obj.Count>1)                    obj[objIndex-1].bathWidth = 0;                    obj[objIndex].bathWidth = bathWidth;                }                else if (item.Item4 != -1) //一个Bath只有一个对应的MO源                {                    bathWidth = uc.Width;                    obj[objIndex].bathWidth = bathWidth;                }                else //Bath没有对应的MO源                {                    bathWidth = 0;                    obj[objIndex].bathWidth = -1;                }                objIndex++;                splitBath = item.Item4;                width -= uc.Width;            }            intervalWidth = width / items.Count;            foreach (var item in obj)            {                //Mo layout                UserControl uc = item.uc as UserControl;                Canvas.SetLeft(uc, left);                uc.DataContext = MOSourceDataList[item.index];                rootCanvas.Children.Add(uc);                bathIntervalCount++;                //bath layout                if(item.bathWidth != -1)                {                    if(bathLeft == -1)                    { bathLeft = left; }                                        if(item.bathWidth>0)                    {                        bathUc = new Bath();                        bathUc.Width = item.bathWidth + ((bathIntervalCount-1) * intervalWidth);                        Canvas.SetLeft(bathUc, bathLeft);                        Canvas.SetTop(bathUc, 220);                        bathUc.SetBinding(Bath.DeviceDataProperty, new Binding("BathData") { Source = MOSourceDataList[item.index] });                        //bathUc.DataContext = MOSourceDataList[item.index - 1];                        rootCanvas.Children.Add(bathUc);                        bathLeft = -1;                        bathIntervalCount = 0;                    }                                    }                left += uc.Width + intervalWidth;            }        }        public class UILayoutDataItem        {            public int index;            public UserControl uc;            public double bathWidth;        }    }}
 |