473,217 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,217 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 5314
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: roberto3214 | last post by:
Hi Guys, How are you? I have recently begun some testing on IIS 6.0 in regards to an asp.net application. After lots of testing I decided to create a simple 1k page size webpage to find out...
2
by: Kallis | last post by:
Hello, I have the following situation when trying to localize my software: I have BIG solution with about 80 projects. In one of the projects I have a number of dialogs (the dialog project :-)...
4
by: Guadala Harry | last post by:
Is there any way for one Session to remove and update objects in another Session? I seriously doubt it, but thought I'd ask. Here's why: I have some data that is unique per user (or per session -...
2
by: needin4mation | last post by:
Hi, I have to decide between a per device and a per license issue. We have several web services that have functions that use things like LOGON_USER. If I have a per device license, does that mean...
7
by: Randy Yates | last post by:
I'm a complete newbie to postgres so please look the other way if these questions are really stupid. Is it legitimate to have one database per data file? For organizational and backup purposes,...
12
by: bruno at modulix | last post by:
Hi I'm currently playing with some (possibly weird...) code, and I'd have a use for per-instance descriptors, ie (dummy code): class DummyDescriptor(object): def __get__(self, obj,...
32
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many...
10
by: Joseph Geretz | last post by:
I need to calculate miles per degree longitude, which obviously depends on latitude since lines of longitude converge at the poles. Via Google, I've come up with the following calculation: ...
9
by: bakxchaixDD | last post by:
I DON'T GET THIS project: Many treadmills output the speed in miles per hour. However, most runners think of their pace in minutes and seconds per mile. Write a program that inputs a decimal...
0
by: raveekumarg | last post by:
hi what is the difference between per seat , per server , per processor and i am new to sql 2000 , pl do explain the same.
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.