Search code examples
wpfcolorscontrolsinputbinding

Change button color using keyboard shortcut C# WPF


I'm getting into some C# WPF, and am creating a simple word processor to convert normal text into wiki markup. I am new to WPF and am having trouble with something seemingly minuscule, and hopefully an easy fix.

I have a Bold button on my main form. When pressed it does what I need it to do, which is turn the selected text to bold and vice versa when pressed again. The Bold button also changes a pretty light blue color when pressed, then back to gray when pressed again. So sweet that works...

//Make Bold MAIN method
    static bool isBold = false;
    public static void boldText()
    {
        if (isBold == false)
        {

            TextSelection ts = MainWindow.thisForm.rtbMain.Selection;
           MainWindow.thisForm.btnBold.Background = Brushes.LightBlue;
            if (!ts.IsEmpty)
            {

                ts.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            }
            isBold = !isBold;

        }
        else
        {

            MainWindow.thisForm.btnBold.Background = Brushes.LightGray;
            TextSelection ts = MainWindow.thisForm.rtbMain.Selection;
            ts.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
            isBold = !isBold;

        }

    }

My issue now is for some reason when I call the code above using an InputBinding the selected text turns bold but the button color doesn't change... whaaaaA? I have created a custom Command, Execute and CanExecute Commands below:

  public class ToolBar
   {
     //Custom Command
           public static RoutedCommand boldShortCut = new RoutedCommand();

        //For use with Keybindings for BOLD command
            static bool canExecute = true;
            public static void myCommandExecute(object sender, ExecutedRoutedEventArgs e)
            {
                boldText();
                canExecute = !canExecute;
            }
            public static void myCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = true;
            }

I then create the KeyGesture and InputBindings in the main form's constructor:

public MainWindow()
        {
            InitializeComponent();
            thisForm = this;
            initializeFeatures();

            KeyGesture kg = new KeyGesture(Key.B, ModifierKeys.Control);
            InputBinding ib = new InputBinding(ToolBar.boldShortCut, kg);
            this.InputBindings.Add(ib);
        }

So this all works but for some reason the Bold button isn't changing color when I use the key gesture (CTRL+B). Is there something I need to do in XAML? Any help would be greatly appreciated and please let me know if anything is unclear or additional info is needed. Thanks guys!


Solution

  • The command you are creating (boldShortCut) is never being setup to do anything. A RoutedCommand only fires an event when it is activated. There needs to be a command binding somewhere up the tree that listens for the event and performs some logic.

    Take a look at this page for an explanation of how routed commands work:
    How to: Create a RoutedCommand

    Additionally, the only reason the text turns bold when you press Ctrl+B is because that is a built-in feature of a RichTextBox. In fact, RichTextBox has a whole lot of features that you can hook toolbar buttons up to very easily. You could save yourself a lot of time by learning how to use a RichTextBox before trying to reimplement its existing features.

    Here is a good page to get you started: RichTextBox Overview

    (For a full list of existing commands that you can hook things up to, see the EditingCommands Class documentation.)