My Prism app needs to insert buttons from several modules into a Shell region. The buttons will be stacked vertically, like the navigation buttons (Mail, Calendar, and so on) in Outlook 2010. I am using custom controls as buttons, so I don't need to worry about templating--my problem is the same as if I was inserting plain radio buttons.
How do set up the region so that the buttons will appear stacked vertically? Thanks for your help.
StackPanel immediately jumps to mind when thinking of stacking items vertically. Unfortunately Prism doesn't support the StackPanel to be a region out of the box. Luckily you can create a RegionAdapter to fix this problem.
Public Class StackPanelRegionAdapter
Inherits RegionAdapterBase(Of StackPanel)
Public Sub New(ByVal behaviorFactory As IRegionBehaviorFactory)
MyBase.New(behaviorFactory)
End Sub
Protected Overrides Sub Adapt(ByVal region As IRegion, ByVal regionTarget As StackPanel)
AddHandler region.Views.CollectionChanged, Sub(sender As Object, e As NotifyCollectionChangedEventArgs)
If e.Action = NotifyCollectionChangedAction.Add Then
For Each element As FrameworkElement In e.NewItems
regionTarget.Children.Add(element)
Next
Else
If e.Action = NotifyCollectionChangedAction.Remove Then
For Each element In e.OldItems
If regionTarget.Children.Contains(element) Then
regionTarget.Children.Remove(element)
End If
Next
End If
End Sub
End Sub
Protected Overrides Function CreateRegion() As Microsoft.Practices.Prism.Regions.IRegion
Return New AllActiveRegion
End Function
End Class
From there you just need to add the mapping in the ConfigureRegionAdapterMappings Override in your bootstrapper.
Protected Overrides Function ConfigureRegionAdapterMappings() As Microsoft.Practices.Prism.Regions.RegionAdapterMappings
Dim mappings = MyBase.ConfigureRegionAdapterMappings()
mappings.RegisterMapping(GetType(Grid), Container.Resolve(Of PrismExtensions.GridRegionAdapter))
mappings.RegisterMapping(GetType(StackPanel), Container.Resolve(Of PrismExtensions.StackPanelRegionAdapter))
Return mappings
End Function
Edit: Found the John Papa link I originally got the code from. (In C# if that's what you're using) Fill My Prism Region, Please
Why not just use an ItemsControl as a region? It stacks items vertically and Prism has builtin region adapters for it. I don't understand why you'd want to use a StackPanel for anything but layout.
An ItemsControl by default uses an ItemsPanel that contains a StackPanel, so it's equivalent to the answers already provided, but without the unnecessary code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With