private void addOrRemoveEvent(SwitchPanelViewModel model, bool isAdd)
 {
     if (model != null)
     {
         if (isAdd)
         {
             model.SwitchesUpdated += Model_SwitchsUpdated;
         }
         else
         {
             model.SwitchesUpdated -= Model_SwitchsUpdated;
         }
     }
 }
 private void refreshSwitchBtns(SwitchPanelViewModel model)
 {
     clearSwitchBtns();
     if (model != null)
     {
         for (int i = 0; i < model.AllSwitchStatus.Count; i++)
         {
             Button  btn     = new Button();
             Binding binding = BindingHelper.CreateBinding(model.AllSwitchStatus[i], BindingMode.OneWay, null);
             btn.SetBinding(Button.DataContextProperty, binding);
             wrapSwitches.Children.Add(btn);
         }
     }
 }
        private void thisDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            SwitchPanelViewModel oldM = e.OldValue as SwitchPanelViewModel;
            SwitchPanelViewModel newM = e.NewValue as SwitchPanelViewModel;

            if (oldM != null)
            {
                addOrRemoveEvent(oldM, false);
            }
            if (newM != null)
            {
                addOrRemoveEvent(newM, true);
            }
            refreshSwitchBtns(ViewModel);
        }