PageRenderTime 8ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/CBR/CBR/Views/File/DeviceConfigView.xaml.cs

#
C# | 56 lines | 45 code | 7 blank | 4 comment | 4 complexity | 9a5aa4e41ea6393845301ea158e3b16b MD5 | raw file
 1using System.Linq;
 2using System.Windows;
 3using System.Windows.Controls;
 4using CBR.Core.Helpers;
 5using CBR.Core.Services;
 6using CBR.ViewModels;
 7using System.IO;
 8using System.Collections.Generic;
 9using CBR.Core.Models;
10
11namespace CBR.Views
12{
13    /// <summary>
14    /// Interaction logic for DeviceConfigView.xaml
15    /// </summary>
16    public partial class DeviceConfigView : UserControl
17    {
18        public DeviceConfigView()
19        {
20            InitializeComponent();
21
22            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
23            {
24                //WorkspaceService.Instance.Settings.DeviceInfoList.Clear();
25
26                if (WorkspaceService.Instance.Settings.DeviceInfoList.Count <= 0)
27                {
28                    if (MessageBox.Show("Your device list is empty, Do you want to load the default one ?", "Warning", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
29                        WorkspaceService.Instance.Settings.DeviceInfoList = (List<DeviceInfo>)XmlHelper.Deserialize(Path.Combine(DirectoryHelper.ApplicationPath, "devices.xml"),
30                            WorkspaceService.Instance.Settings.DeviceInfoList.GetType());
31                }
32                this.DataContext = new DeviceConfigViewModel(WorkspaceService.Instance.Settings.DeviceInfoList);
33            }
34        }
35
36        private void btnSave_Click(object sender, RoutedEventArgs e)
37        {
38            DeviceConfigViewModel dc = this.DataContext as DeviceConfigViewModel;
39            WorkspaceService.Instance.Settings.DeviceInfoList = dc.Data.ToList<Core.Models.DeviceInfo>();
40
41            XmlHelper.Serialize(Path.Combine(DirectoryHelper.ApplicationPath, "devices.xml"), WorkspaceService.Instance.Settings.DeviceInfoList);
42        }
43
44        private void btnAddDevice_Click(object sender, RoutedEventArgs e)
45        {
46            DeviceConfigViewModel dc = this.DataContext as DeviceConfigViewModel;
47            dc.Data.Add(new Core.Models.DeviceInfo(this.tbModel.Text, this.tbManufacturer.Text));
48        }
49
50        private void btnDeleteDevice_Click(object sender, RoutedEventArgs e)
51        {
52            DeviceConfigViewModel dc = this.DataContext as DeviceConfigViewModel;
53            dc.Data.Remove(this.lbDevices.SelectedItem as DeviceInfo);
54        }
55    }
56}