void SaveConfig(Config config)
 {
     System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(Config));
     try
     {
         System.IO.File.Delete("config.xml");
     }
     catch { ;}
     sr.Serialize(System.IO.File.OpenWrite("config.xml"), config);
 }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {



            if (App.Current.Properties.Contains("LightCollection"))
            {
                this.dictStreetLightBindingInfos = App.Current.Properties["LightCollection"] as Dictionary<string, StreetLightBindingData>;
                this.dictStreetLightBindingInfosOriginal = App.Current.Properties["LightCollectionOriginal"] as Dictionary<string, StreetLightBindingData>;
            }
            System.Collections.Generic.Dictionary<string, StreetLightBindingData> List = new Dictionary<string, StreetLightBindingData>(); System.Collections.Generic.Dictionary<string, StreetLightBindingData> list = new Dictionary<string, StreetLightBindingData>();
            if (!System.IO.File.Exists("config.xml"))
            {
                Config config = new Config();
                
                foreach (UIElement element in this.LayoutRoot.Children)
                {
                  
                    if (element is LedButton)
                    {
                        LedButton btn = element as LedButton;
                        list.Add(btn.Text, new StreetLightBindingData() { DevID = btn.Text,OriginalDevID=btn.Text });
                        btn.Tag = btn.Text;
                    }

                  
                }

                config.StreetLightBindingDatas = list.Values.ToArray();
                config.Scenariors = new List<Scenarior>();
                //System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(Config));
                //sr.Serialize(System.IO.File.OpenWrite("config.xml"), config);
                SaveConfig(config);
                LedConfig = config;
                this.listScene.ItemsSource = LedConfig.Scenariors;
            }
            else
            {
                //System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(Config));
                //Config config = sr.Deserialize(System.IO.File.OpenRead("config.xml")) as Config;
                LedConfig = LoadConfig();
                foreach (StreetLightBindingData data in LedConfig.StreetLightBindingDatas)
                    list.Add(data.OriginalDevID, data);

                this.listScene.ItemsSource = LedConfig.Scenariors;
            }

            foreach (UIElement element in this.LayoutRoot.Children)
            {

                if (element is LedButton)
                {
                    LedButton btn = element as LedButton;

                    StreetLightBindingData temp;
                    string devid = "";
                    btn.Tag = list[btn.Text].OriginalDevID;
                    devid = list[btn.Text].DevID;
                    if (!dictStreetLightBindingInfos.ContainsKey(devid))
                    {

                        temp = new StreetLightBindingData() { DevID = devid,OriginalDevID=btn.Tag.ToString() ,DimLevel = 20, IsEnable = false };

                        dictStreetLightBindingInfos.Add(temp.DevID, temp);
                        dictStreetLightBindingInfosOriginal.Add(temp.OriginalDevID, temp);
                    }
                    else

                        temp = dictStreetLightBindingInfos[devid] as StreetLightBindingData;



                    //   temp.IsEnable = true;
                    
                    Binding binding = new Binding() { Path = new PropertyPath("IsEnable"), Converter = converter };

                    btn.SetBinding(Button.ForegroundProperty, binding);
                    //   btn.SetBinding(ForegroundProperty, binding);
                    // btn.Content = null;
                    btn.SetBinding(LedButton.TextProperty, new Binding("DevID"));
                    btn.SetBinding(LedButton.IsEnabledProperty, new Binding("IsEnable"));
                    btn.SetBinding(LedButton.IsCheckedProperty, new Binding("IsChecked") { Mode = BindingMode.TwoWay });
                    // btn.Foreground = btn.Foreground;
                    btn.DataContext = temp;
                    btn.Text = temp.DevID;
              //      btn.Text = btn.Text;
                   btn.IsChecked = temp.IsChecked;
                    //   btn.IsChecked = false;
                }
                //if (element is Button)
                //{
                //    try
                //    {
                //        Button btn = (Button)element;
                //        btn.Name = "btn" + btn.Content;
                //        //   btn.Width = btn.Height = 84;
                //        btn.Click += btn_Click;
                //        StreetLightBindingData temp = new StreetLightBindingData() { DevID = btn.Content.ToString(), DimLevel = 20, IsEnable = false };
                //        dictStreetLightBindingInfos.Add(btn.Content.ToString(), temp);


                //        //   temp.IsEnable = true;
                //        btn.DataContext = temp;

                //        Binding binding = new Binding() { Path = new PropertyPath("IsEnable"), Converter = converter };

                //        btn.SetBinding(Button.ForegroundProperty, binding);
                //        //   btn.SetBinding(ForegroundProperty, binding);
                //        btn.Content = null;
                //        btn.SetBinding(Button.ContentProperty, new Binding("DevID"));
                //        btn.SetBinding(Button.IsEnabledProperty, new Binding("IsEnable"));
                //        //  btn.GetBindingExpression(ForegroundProperty).UpdateSource();

                //    }
                //    catch (Exception ex)
                //    {
                //        System.Diagnostics.Debug.Print(ex.Message);
                //    }
                //}


            } // foeach

            App.Current.Properties["LightCollection"] = dictStreetLightBindingInfos;
            App.Current.Properties["LightCollectionOriginal"] = dictStreetLightBindingInfosOriginal;
            Initial();
           
        }