// Update the selected color picker based on the value of selectedButton.
        private void RefreshSelectedColorPicker()
        {
            LoadFromConfigDelegateArgs args = CurrentSMXDevice.singleton.GetState();

            ColorButton[] buttons = getColorPickerButtons();

            // If our selected button isn't enabled (or no button is selected), try to select a
            // different one.
            if (selectedButton == null || !selectedButton.isEnabled(args))
            {
                foreach (ColorButton button in buttons)
                {
                    if (button.isEnabled(args))
                    {
                        selectedButton = button;
                        break;
                    }
                }
            }

            // Tell the buttons which one is selected.
            foreach (ColorButton button in buttons)
            {
                button.IsSelected = button == selectedButton;
            }

            // Tell the color slider which button is selected.
            AutoLightsColor.colorButton = selectedButton;
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            // Add our WndProc hook.
            HwndSource source = (HwndSource)PresentationSource.FromVisual(this);

            source.AddHook(new HwndSourceHook(WndProcHook));

            Version1.Content = "SMXConfig version " + SMX.SMX.Version();
            Version2.Content = "SMXConfig version " + SMX.SMX.Version();

            AutoLightsColor.StartedDragging += delegate() { showAutoLightsColor.Start(); };
            AutoLightsColor.StoppedDragging += delegate() { showAutoLightsColor.Stop(); };
            AutoLightsColor.StoppedDragging += delegate() { showAutoLightsColor.Stop(); };

            CreateThresholdSliders();

            // This doesn't happen at the same time AutoLightsColor is used, since they're on different tabs.
            Diagnostics.SetShowAllLights += delegate(bool on)
            {
                if (on)
                {
                    showAutoLightsColor.Start();
                }
                else
                {
                    showAutoLightsColor.Stop();
                }
            };

            SetAllPanelsToCurrentColor.Click += delegate(object sender, RoutedEventArgs e)
            {
                // Get the color of the selected color button, and apply it to all other buttons.
                Color color = selectedButton.getColor();

                ColorButton[] colorButtons = getColorPickerButtons();
                foreach (ColorButton button in colorButtons)
                {
                    // Only apply this to panel colors, not the floor color.
                    if ((button as PanelColorButton) == null)
                    {
                        continue;
                    }

                    button.setColor(color);
                }

                CurrentSMXDevice.singleton.FireConfigurationChanged(null);
            };

            // Listen to clicks on the panel color buttons.
            ColorButton[] buttons = getColorPickerButtons();
            foreach (ColorButton button in buttons)
            {
                button.Click += delegate(object sender, RoutedEventArgs e)
                {
                    ColorButton clickedButton = sender as ColorButton;
                    selectedButton = clickedButton;

                    RefreshSelectedColorPicker();
                };
            }
        }