WPF App design: How to handle selection events without coupling

Here's the problem:

The main window of the application contains several panels that may contain selectable controls such as listboxes or listviews.

When the user presses a key combination (ex: CTRL+A) or presses a specific button, I'd like to identify all the selected items from the last selected container and execute a specific operation on them.

An example:

Imagine the application is a store front that displays items of diferent types or categories in diferent listviews. When user presses CTRL+A I'd like to add those items to the shopping cart.

How can I achieve this without tightly coupling all the listboxes with the main window I'd like to write the code once and whenever a new listbox is added I don't want to write more code to wire events from it.

How I thought I could do:

I was thinking that if the listboxes/listviews were able to buble up "avalon events" when items were selected on it, I'd be able to just write an event handler for those events in the main window, and also a command handler to perform the operation on the last remembered selection. Unfortunatly I don't think listviews/listbox buble up these events. What should I do then Subclass the controls Is there another way

Thanks in advance,

-Rob.



Answer this question

WPF App design: How to handle selection events without coupling

  • MA2005

    You could build a registration mechanism for registering the different list boxes, using a custom attached property if necessary. Or whenever a selection changes, have the individual listbox add/remove the item from a master selection list.

    Selector.SelectionChanged is a bubbling event, so seems like that would be another potential solution, although you seemed indicate you tried this and it didn't work (why not ). Thanks.



  • Mike Hollibuagh

    Steps 1 and 2 are fine because I write that just once. But step 3 is a big problem:

    For each new element (new listbox) added, code will have to be written to add that CommandBinding to the CommandBindings property of that specific element.

    This aditional code will also have to use the name of the new element. If you remove the listbox from the UI then you will have to remove that code.

    I am looking for a better solution.

  • SteeleWORX

    I didn't know there was a bubling event raised when the listbox selection changed! Very good! That allows me to create a handler for that in the MainWindow to add the selection to a master collection.

    Adding or removing listboxes will require no change at all !

    Thanks a lot !

  • Michael Baxter

    I think what you are looking for is CommandBinding:

    1) Create a RoutedCommand with the KeyGesture that defines your action (^A).

    2) Create a CommandBinding to map that to a method.

    3) Add the CommandBinding to the elements you want to support that action.

    Your method will now be called when that gesture is used with the various elements having focus (or enclosed elements). One of the arguments to the method is the sender which can allow you to locate the list you care about.



  • WPF App design: How to handle selection events without coupling