471,357 Members | 1,094 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

Re: Per-UserControl key bindings in WPF

Hi All,

I have a custom UserControl that is used as the list item in a listbox
in my main WPF window.

Is it possible to have key bindings that apply to every instance of
the user control? For example, I have a keybinding Alt+R for a button
in the UserControl. However when there are multiple instances of the
UserControl in the listbox in my main WPF window, pressing Alt+R just
shifts the keyboard focus to the button in the first user control.
Pressing Alt+R subsequently just makes the focus cycle through the
same button in other user controls.

Thanks,
Sundar
Aug 14 '08 #1
6 5210
I tried the suggestion in "http://neilmosafi.blogspot.com/2007/04/
default-buttons-in-wpf-and-multiple.html". But still the same
behavior :(
Aug 14 '08 #2
On Thu, 14 Aug 2008 08:39:14 -0700, sundarvenkata
<su***********@gmail.comwrote:
Hi All,

I have a custom UserControl that is used as the list item in a listbox
in my main WPF window.

Is it possible to have key bindings that apply to every instance of
the user control?
Key input is necessarily focus-based.

But, you could add a method in your UserControl sub-class that the parent
window can call. Then, in the parent window filter the keyboard input so
that it sees key input before its children do, and have the parent window
call the UserControl's method on each child control when the appropriate
key is pressed.

I don't know the exact override you'd use for a WPF window to do this, but
in the Forms Control class, a parent Form instance could override the
ProcessKeyPreview() method to be given the chance to see key input before
child controls handle it. Maybe there's something similar in the WPF
classes.

Pete
Aug 14 '08 #3
I see your point. But doing so defeats the reusability of the
UserControl. Every time I use this control in some other form, I need
to ensure that the parent control calls the appropriate methods in the
child control to handle keyboard events. Is it possible to scope the
key handling such that it is handled only by the user control and not
passed up the control hierarchy?
Aug 14 '08 #4
On Thu, 14 Aug 2008 09:12:27 -0700, sundarvenkata
<su***********@gmail.comwrote:
I see your point. But doing so defeats the reusability of the
UserControl. Every time I use this control in some other form, I need
to ensure that the parent control calls the appropriate methods in the
child control to handle keyboard events. Is it possible to scope the
key handling such that it is handled only by the user control and not
passed up the control hierarchy?
Maybe I misunderstood your question. If you only want the UserControl to
handle the keyboard input, then what do you mean by "to have key bindings
that apply to every instance of the user control"? The way I read that,
you want a single key input to cause something to happen in every instance
of the UserControl that you've made.

Are you saying that's not actually what you want? If so, could you
elaborate on what's wrong with the default "cycle through the buttons"
behavior?

If I didn't misunderstand, then I think you could implement this in the
UserControl only, but I would say that would be a bad design. In
particular, of course any given UserControl receiving key input could
inspect all of the children of its parent to see if there are other
UserControl instances that it can forward the key input to. But then what
happens when someone wants to use more than one of those UserControl
instances in a situation when they _don't_ want that behavior?

Putting the logic in the parent seems to me to be a simpler, more
adaptable, more sensible design.

Pete
Aug 14 '08 #5
I get your point. I had some weird idea that I can encapsulate a
keyboard response behavior of the UserControl in the definition of the
UserControl itself. But since keyboard inputs are usually handled at
the form level, I think I should go with your suggestion.
Aug 14 '08 #6
On Aug 14, 8:35*pm, sundarvenkata <sundarvenk...@gmail.comwrote:
I get your point. I had some weird idea that I can encapsulate a
keyboard response behavior of the UserControl in the definition of the
UserControl itself. But since keyboard inputs are usually handled at
the form level, I think I should go with your suggestion.
If you really want to do that, you can - you can track changes to your
Parent property (it's a dependency property), and when it changes,
follow the parent chain all the way to the topmost element, and
subscribe to its KeyDown event (and unsubscribe from event of your
previous parent, if any).

But I would second the notion that it is not, in general, a good idea
to do that sort of thing.
Aug 14 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by roberto3214 | last post: by
2 posts views Thread by Kallis | last post: by
2 posts views Thread by needin4mation | last post: by
7 posts views Thread by Randy Yates | last post: by
12 posts views Thread by bruno at modulix | last post: by
32 posts views Thread by Matias Jansson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.