StockerMonitor.xaml.cs 7.28 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace HHWCSHost.Controls
{
    /// <summary>
    /// StockerMonitor.xaml 的交互逻辑
    /// </summary>
    public partial class StockerMonitor : UserControl
    {
        public string ControlName { get; set; }
        public StockerMonitor(int columns,bool two,bool direction,int maxWidth,int maxHeight)
        {
            InitializeComponent();
            this.MaxWidth = maxWidth;
            this.MaxHeight = maxHeight;
            //grid.ma = MaxWidth;
            //grid.Height = MaxHeight;
            Render(columns,two,direction);
        }

        private void Render(int columns,bool two,bool direction)
        {
            if (columns <= 0)
            {
                return;
            }
            for (int i = 0; i < columns; i++)
            {
                var a = new ColumnDefinition();
                a.Width = new GridLength(100/columns,GridUnitType.Star);
                grid.ColumnDefinitions.Add(a); 
            }
            if (two)
            {
                var a = new RowDefinition();
                var b = new RowDefinition();
                a.Height = new GridLength(50, GridUnitType.Star);
                b.Height = new GridLength(50, GridUnitType.Star);
                grid.RowDefinitions.Add(a);
                grid.RowDefinitions.Add(b);
                if (direction)
                {
                    //先画堆垛机
                    RenderStocker(0,0);
                    RenderLocation(columns, 1);
                }
                else
                {
                    RenderStocker(0, 1);
                    RenderLocation(columns, 0);
                }
            }
            else
            {
                var a = new RowDefinition();
                var b = new RowDefinition();
                var c = new RowDefinition();

                a.Height = new GridLength(33, GridUnitType.Star);
                b.Height = new GridLength(33, GridUnitType.Star);
                c.Height = new GridLength(33, GridUnitType.Star);
                grid.RowDefinitions.Add(a);
                grid.RowDefinitions.Add(b);
                grid.RowDefinitions.Add(c);
                RenderStocker(0, 1);
                RenderLocation(columns, 0);
                RenderLocation(columns, 2);
            }
        }

        private void RenderStocker(int columnIndex,int rowIndex)
        {
            Border border = new Border();
            border.BorderThickness = new Thickness(0, 0, 1, 1);
            border.BorderBrush = Brushes.Blue;
            border.Name = "stocker";
            border.Background = Brushes.White;
            Image image = new Image();
            BitmapImage bi3 = new BitmapImage();
            bi3.BeginInit();
            bi3.UriSource = new Uri("/Content/image/Tractor.ico", UriKind.Relative);
            bi3.EndInit();
            image.Source = bi3;
            image.Stretch = Stretch.Fill;
            border.Child = image;
            border.SetValue(Grid.RowProperty, rowIndex);
            border.SetValue(Grid.ColumnProperty, columnIndex);
            grid.Children.Add(border);
        }

        private void RenderLocation(int columns,int rowIndex)
        {
            //再画货架
            for (int i = 0; i < columns; i++)
            {
                Border border = new Border();
                border.BorderThickness = new Thickness(0, 0, 1, 1);
                border.BorderBrush = Brushes.Blue;
                border.Name = "location"+rowIndex.ToString()+i.ToString();
                border.Background = Brushes.White;
                Image image = new Image();
                BitmapImage bi3 = new BitmapImage();
                bi3.BeginInit();
                bi3.UriSource = new Uri("/Content/image/List.JPG", UriKind.Relative);
                bi3.EndInit();
                image.Source = bi3;
                image.Stretch = Stretch.Fill;
                border.Child = image;
                border.SetValue(Grid.RowProperty, rowIndex);
                border.SetValue(Grid.ColumnProperty, i);
                grid.Children.Add(border);
            }
        }

        /// <summary>
        /// 设置位置以及背景色,1白色,2,3蓝色,4红色
        /// </summary>
        /// <param name="flag"></param>
        /// <param name="columnIndex"></param>
        public void SetStocker(int flag,int columnIndex)
        {
            Border border = GetUIElement(0, 0, "stocker");
            if (border == null) return;
            switch (flag)
            {
                case 1:
                    border.Background = Brushes.White;
                    break;
                case 2:
                    border.Background = Brushes.Blue;
                    break;
                case 3:
                    border.Background = Brushes.Blue;
                    break;
                case 4:
                    border.Background = Brushes.Red;
                    break;
                default:
                    border.Background = Brushes.White;
                    break;
            }
            border.SetValue(Grid.ColumnProperty, columnIndex);
        }

        /// <summary>
        /// 设置货架监控 1,
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <param name="rowIndex"></param>
        /// <param name="flag"></param>
        public void SetLocation(int columnIndex,int rowIndex,int flag)
        {
            Border border = GetUIElement(columnIndex, rowIndex, null);
            if (border == null) return;
            Image image = (Image)border.Child;
            BitmapImage bi3 = new BitmapImage();
            bi3.BeginInit();
            switch (flag)
            {
                case 0:
                    bi3.UriSource = new Uri("/Content/image/List.JPG", UriKind.Relative);
                    break;
                case 1:
                    bi3.UriSource = new Uri("/Content/image/取货架.png", UriKind.Relative);
                    break;
                case 2:
                    bi3.UriSource = new Uri("/Content/image/送货架.png", UriKind.Relative);
                    break;
                default:
                    bi3.UriSource = new Uri("/Content/image/List.JPG", UriKind.Relative);
                    break;
            }
            bi3.EndInit();
            image.Source = bi3;
            image.Stretch = Stretch.Fill;
            border.Child = image;
        }

        private Border GetUIElement(int columnIndex, int rowIndex,String name)
        {
            String uiName = "";
            if (String.IsNullOrEmpty(name))
            {
                 uiName = "location" + rowIndex + columnIndex;
            }
            else
            {
                uiName = name;
            }
            foreach (var item in grid.Children)
            {
                Border border = (Border)item;
                if (border.Name == uiName)
                {
                    return border;
                }
            }
            return null;
        }

    }
}