Multiple View Instances - Selection

Topics: Prism v4 - WPF 4
Feb 25, 2011 at 10:53 AM

Hi,

I have placed multiple instances of the same view (search results with different filter settings) to a region. The view contains a ListView control, which allows to select items. 

If an item is selected in view instance 1, another item can be selected in view instance 2, since they are independant. I would like to avoid that, so that only one item can be selected at the same time. How to accomplish that in PRISM manner?

 

Regards,

Andreas

Developer
Feb 25, 2011 at 2:21 PM

Hi Andreas,

In order to achieve that, you should call the UnselectAll method on your ListView control when the SelectionChanged event is fired in the other ListView control.

If your views are on different modules, you could use one of the approaches for communicating between loosely coupled components, such as the event aggregator, to facilitate this interaction.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Feb 25, 2011 at 3:13 PM

Hi Guido,

thanks for your reply.

Since its all the SAME view (just created several instances), I cannot simply call UnselectAll() on the view, because then NO item could be selected effectively (the item which really IS selected and should be shown as selected would also be unselected). So my problem is that I don't know how to identify the one view _instance_ on which the selection occured, and the other view _instances_ which should be unselected. Another thing to say is that I have injected the view instances programmatically (using a controller class and service locator). So I also don't know how much view instances are placed to the region at compile time..  

Any guess?


Regards,

Andreas

Developer
Feb 25, 2011 at 3:57 PM

Hi Andreas,

In that case, you could store an identifier in the ListView.Tag property, and check if it matches before calling the UnselectAll method.

Another possibility would be to use the Event Aggregator's filter functionality to check for the Tag property or a property in the controller (you can read more about the event aggregator in this chapter from the Prism msdn documentation, as well as check the Event Aggregation QuickStart). If you use the MVVM pattern, you could store a property in your ViewModel and bind the ListView.SelectedItem property to it. That way, you could subscribe to the aforementioned event in your ViewModel and perform this interaction there.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi