Change button color using keyboard shortcut C# WPF -
i'm getting c# wpf, , creating simple word processor convert normal text wiki markup. new wpf , having trouble seemingly minuscule, , easy fix.
i have bold button on main form. when pressed need do, turn selected text bold , vice versa when pressed again. bold button changes pretty light blue color when pressed, gray when pressed again. sweet 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 reason when call code above using inputbinding selected text turns bold button color doesn't change... whaaaaa? have created custom command, execute , canexecute commands below:
public class toolbar { //custom command public static routedcommand boldshortcut = new routedcommand(); //for use keybindings 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 create keygesture , inputbindings in 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 works reason bold button isn't changing color when use key gesture (ctrl+b). there need in xaml? appreciated , please let me know if unclear or additional info needed. guys!
the command creating (boldshortcut
) never being setup anything. routedcommand
fires event when activated. there needs command binding somewhere tree listens event , performs logic.
take @ page explanation of how routed commands work:
how to: create routedcommand
additionally, reason text turns bold when press ctrl+b because built-in feature of richtextbox
. in fact, richtextbox
has whole lot of features can hook toolbar buttons easily. save lot of time learning how use richtextbox
before trying reimplement existing features.
here page started: richtextbox overview
(for full list of existing commands can hook things to, see editingcommands class documentation.)
Comments
Post a Comment