Interaction logic for DeviceItem.xaml
Наследование: System.Windows.Controls.UserControl
        private Controls.DeviceItem AddDeviceItem(DeviceDescription device)
        {
            var deviceItem = new Controls.DeviceItem(this, device);

            deviceItem.Moved        += DeviceItem_Updated;
            deviceItem.Resized      += DeviceItem_Updated;
            deviceItem.CloseClicked += DeviceItem_CloseClicked;

            // Add to Canvas
            shopCanvas.Children.Add(deviceItem);

            // Add to DeviceItems collection (to update data)
            DeviceItems.Add(deviceItem);

            return(deviceItem);
        }
        private void SaveDeviceItemLocation(Controls.DeviceItem deviceItem)
        {
            List <DeviceItemLocation> locations = null;

            var json = Properties.Settings.Default.ShopStatusItemLocations;

            if (!string.IsNullOrEmpty(json))
            {
                locations = JSON.ToType <List <DeviceItemLocation> >(json);
            }

            if (locations == null)
            {
                locations = new List <DeviceItemLocation>();
            }

            double x = Canvas.GetLeft(deviceItem);
            double y = Canvas.GetTop(deviceItem);

            double width  = deviceItem.ActualWidth;
            double height = deviceItem.ActualHeight;

            var location = locations.Find(o => o.DeviceId == deviceItem.Device.UniqueId);

            if (location == null)
            {
                location = new DeviceItemLocation();
                locations.Add(location);
            }

            location.DeviceId = deviceItem.Device.UniqueId;
            location.X        = x;
            location.Y        = y;
            location.Height   = height;
            location.Width    = width;

            json = JSON.FromList <DeviceItemLocation>(locations);
            if (!string.IsNullOrEmpty(json))
            {
                Properties.Settings.Default.ShopStatusItemLocations = json;
                Properties.Settings.Default.Save();
            }
        }
        private void RemoveDeviceItemLocation(Controls.DeviceItem deviceItem)
        {
            var json = Properties.Settings.Default.ShopStatusItemLocations;

            if (!string.IsNullOrEmpty(json))
            {
                var locations = JSON.ToType <List <DeviceItemLocation> >(json);

                int i = locations.FindIndex(o => o.DeviceId == deviceItem.Device.UniqueId);
                if (i >= 0)
                {
                    locations.RemoveAt(i);
                }

                json = JSON.FromList <DeviceItemLocation>(locations);
                if (!string.IsNullOrEmpty(json))
                {
                    Console.WriteLine(json);

                    Properties.Settings.Default.ShopStatusItemLocations = json;
                    Properties.Settings.Default.Save();
                }
            }
        }
 private void DeviceItem_Updated(Controls.DeviceItem item)
 {
     SaveDeviceItemLocation(item);
 }
        private void DeviceItem_CloseClicked(Controls.DeviceItem item)
        {
            AddListItem(item.Device);

            RemoveDeviceItem(item);
        }
 private void RemoveDeviceItem(Controls.DeviceItem deviceItem)
 {
     shopCanvas.Children.Remove(deviceItem);
     DeviceItems.Remove(deviceItem);
     RemoveDeviceItemLocation(deviceItem);
 }