Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 891163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:50:50+00:00 2026-05-15T13:50:50+00:00

I’ve created a ToggleButton to show and hide a piece of UI and I’ve

  • 0

I’ve created a ToggleButton to show and hide a piece of UI and I’ve sent its Content to an icon.

I now want to add a shortcut key to the ToggleButton but I’m unsure how to do with without binding a command as well. Since all I am doing is binding to the IsChecked state, I don’t need a command to do any other functionality and creating an empty one seems incorrect.

Here is my ToggleButton as it stands currently non-functional and not responding when I press the indicated shortcut key.

<ToggleButton ToolTip="Command History"
              MinWidth="24"
              IsChecked="{Binding IsShowHistoryChecked}"
              Margin="7">
    <ToggleButton.InputBindings>
        <KeyBinding Gesture="Ctrl+H" />
    </ToggleButton.InputBindings>
    <Image Source="/Amuse;component/Images/ComHistory256.png"
           Width="24" />
</ToggleButton>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:50:51+00:00Added an answer on May 15, 2026 at 1:50 pm

    An InputBinding on the ToggleButton itself won’t do the trick, but there are two good WPF solutions for your problem:

    1. Use a RoutedCommand that updates the model.
    2. Register an access key.

    Why your InputBinding solution won’t work

    The InputBinding you have defined currently won’t work because it doesn’t list a command. It is easy to create a command that toggles a button, as follows:

    public void Execute(object parameter)
    {
      ((ToggleButton)parameter).IsChecked = !((ToggleButton)parameter).IsChecked;
    }
    

    However this will not achieve what you are looking for. You want Ctrl-H to toggle your button even when the button is not focused. An InputBinding will not accomplish this for you, since it only works when the button has focus. I will now discuss two solutions you can use.

    Option 1: Use a RoutedCommand that updates the model

    The whole point of WPF’s architecture is that you never will need to “toggle a button” in the first place: Conceptually all keyboard and mouse actions in WPF should serve to toggle a bound property in your model or view model. The ToggleButton then just becomes the mechanism for accepting mouse clicks, but need not be the only one.

    The name you chose for your “IsShowHistoryChecked” property indicates a fundamental problem in the way you’re conceptualizing your view model. Your view model should not be designed around the view – rather, it should expose logical concepts such as a “ShowHistory” property. The view may bind this to a CheckBox or ToggleButton, or it may choose some other mechanism, or it may not expose it at all. The whole point of data binding and view models is that when you create the view model you don’t care what the actual view will be like. In fact, during automated unit testing there will be no checkbox so “IsShowHistoryChecked” would clearly be a real misnomer.

    So let’s say you’ve properly separated your view from your view model and you have a “ShowHistory” property. First implement a “ToggleShowHistory” command in your view model that, when executed, toggles the ShowHistory property. Now all you have to do is assign this command an InputBinding of Ctrl-H at the view level and you’re done. Even if the ToggleButton is removed from the view entirely the InputBinding will still take effect and Ctrl-H will still work. Nirvanna.

    Option 2: Register an access key

    Windows has a standard mechanism for associating keys with arbitrary buttons and labels, which is the “access key” concept. If you register an access key of “h” on the ToggleButton, pressing Alt-H will toggle the button, and so will just plain H if you don’t have a TextBox or another control accept it first.

    It is very simple to register the access key in code:

    AccessKeyManager.Register("h", togleButton);
    

    This registers “h” as the access text. Now if the user presses Alt-H anywhere in scope (or plain “h” if isn’t handled by a TextBox), your button will toggle.

    You can also do it in XAML. If you’re showing text in your button, just use an underline before the access key letter:

    <Button Text="Show _History" ... />
    

    If you’re showing something other than just text, include a hidden AccessText element in your button content:

    <Button ...>
      <Grid>
        <AccessText Text="_h" Visibility="Collapsed" />
        <Image ...>
      </Grid>
    </Button>
    

    In case you’re wondering, WPF has no built in mechanism to request that AccessKey registrations respond to Ctrl instead of Alt, so this will not allow you to set Ctrl-H as the access key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
Basically, what I'm trying to create is a page of div tags, each has
In order to apply a triggered animation to all ToolTip s in my app,
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.