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

  • SEARCH
  • Home
  • 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 361529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:53:48+00:00 2026-05-12T12:53:48+00:00

One of the things I really like with WPF is the extent to which

  • 0

One of the things I really like with WPF is the extent to which my views can be built declaratively, ie. using XAML rather than code-behind.

Now I’m really stumped by InputBindings, because their CommandParameters don’t accept bindings. I imagine my case is pretty generic and straightforward, but I cannot see how I can do it without resorting to code-behind. Consider:

<ListBox Name="casingsListBox" ItemsSource="{Binding Path=Casings}" SelectedValuePath="Id">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Title}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.InputBindings>
        <!-- Doesn't work: -->
        <MouseBinding Gesture="LeftDoubleClick"
                      Command="ApplicationCommands.Open"
                      CommandParameter="{Binding RelativeSource={RelativeSource Self} Path=SelectedValue}"/>
    </ListBox.InputBindings>
</ListBox>

This will not work, since the binding expression for the MouseBinding’s CommandParameter is illegal.

I ask myself: what’s the point of adding a mouse-click gesture to a listbox if I cannot get to the selected value?

This can be easily solved using a code-behind event handler, of course, or by having the command consumer extract the id from the command source, but there are several reasons why this is undesirable. Apart from the fact that droves of code-behind code defeats (some of) the purpose of WPF in the first place, it makes the UI designers working in Expression Blend less empowered. And dammit, my command parameter shall be an id, not some UI element!!

Subjective: Having browsed SO for a while, I’m struck by the amount of code I see in the WPF-related questions. I get the feeling we developers stick to our old habits and happily hack away in the code-behind file rather than trying to utilize the fresh take on UI building that WPF is supposed to represent. What do you think?

But most importantly: can anyone show me a code-free workaround for this seemingly trivial problem? Preferably without terrible hacks like this one.

  • 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-12T12:53:49+00:00Added an answer on May 12, 2026 at 12:53 pm

    I wrote a markup extension that allows an InputBinding‘s Command to be databound :

    <KeyBinding Modifiers="Control" Key="E" Command="{input:CommandBinding EditCommand}"/>
    

    Your situation is slightly different since you want to bind the CommandParameter, but you can probably adapt my code to fit your case. Note that this code uses private reflection, which only works in full-trust, and can be broken in later versions of WPF (actually it is broken in WPF 4.0… I can post a modified version if you need it).

    Another option is to use the CommandReference class that can be found in the MVVM toolkit :

    <Window.Resources>
        <c:CommandReference x:Key="EditCommandReference" Command="{Binding EditCommand}"/>
    </Window.Resources>
    
    ...
    
    <KeyBinding Modifiers="Control" Key="E" Command="{StaticResource EditCommandReference}"/>
    

    Again, this is for binding the Command property, but can probably be adapted to bind the CommandParameter…

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

Sidebar

Related Questions

I'm using Quartz(1.6.6) , and it's a great system , but one things really
One of the things that I really like in Windows API is overlapped I/O.
One of the things I really like about django is the way the server
I am new to using numpy and one thing that I really don't understand
Apparently MapReduce queries are one of the slowest things one can do in MongoDB
One of the things I would like to follow is to stick to the
I'm a long time winforms developer and one of the things I really liked
One of the things that always stumbles me when working with WPF is the
One of the things I really miss from javascript/jquery work is being able to
I am coding a rhythm game and one of the things I really want

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.