473,414 Members | 1,563 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,414 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 5331
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.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.