Handling selected item in ListBox Region

Mar 20, 2015 at 7:31 AM

I have a prism wpf application. In the shell I have defined ListBox as LeftPaneRegion.
The views are added to this region using ViewExport Attribute
[ViewExport(RegionName =LeftPaneRegion)]
public partial class MyView1: IView, IActiveAware

[ViewExport(RegionName =LeftPaneRegion)]
public partial class MyView1: IView, IActiveAware
When the application loads I want to select one of the view as default.
How to achieve it?

Mar 21, 2015 at 2:08 AM
If driven from the module itself, should be able to just set the IsActive property of the view and raise the IsActiveChanged event.
Mar 23, 2015 at 4:34 AM
Hi BrianNoyes,

Thanks for the response. I tried setting isActive to true in view's InitializeComponent function. But changing the isActive to true does not change IsSelected property of listboxitem. I want both of them to be in sync.

Mar 24, 2015 at 1:08 PM
Hi Sharda,
I apologize, I was remembering wrong. The region implementation will set the IsActive flag on a view that is activated by the user through the interactions of the container control, which was the scenario it was designed around, but there is not currently a way to get the region to switch the active view for you from a contained view or view model.

I think this could be achieved through a custom RegionBehavior. I'll add an item to our backlog at http://github.com/prismlibrary for this, but if you want to take a look into creating a RegionBehavior that does this and submit a pull request over there that would be great.
Mar 24, 2015 at 1:18 PM
On second thought, I'm not sure if that would be the best approach. One workaround at this point could also be to use PubSubEvents to notify the ViewModel for the parent region that a child view would like to become active, and leave it up to that parent ViewModel to do the activation. This may be better overall because there may be transient state in the current view for something like a TabControl where letting a child take control of the navigation by demanding it become active may be the wrong thing to do. The better way is to use the RequestNavigate means of activating an appropriate view within a region. That can be called from anywhere in a loosely coupled way, and leaves the views that are participating in the region in control of things like should the current view be navigated away from.
Mar 25, 2015 at 4:29 AM
Hi Brian,

Thanks for the suggestion. Currently I think using RequestNavigate also this problem will arise. But I would have to try.

Currently I have implemented one workaround.
The ChildView is implementing IActiveAware and IPartImportsSatisfiedNotification. IsActive Property is bound to IsSelectedItem of Listbox Region
    <Style x:Key="RegionListItemContainerStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="IsSelected" Value="{Binding IsActive, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
On ImportSatisfy event in the child view, I am setting IsActive to true, which will update the ListboxItem IsSelected property and hence on UI we see that item is selected.

Mar 25, 2015 at 1:13 PM
Actually I like your solution even better! Leverage the power of data binding for what it was intended for - synchronizing state of two things. Well done.