Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism: Stacking controls in a region?

Tags:

prism

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.

like image 774
David Veeneman Avatar asked Nov 28 '22 03:11

David Veeneman


2 Answers

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

like image 145
Matt.P Avatar answered Feb 15 '23 23:02

Matt.P


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.

like image 25
Anderson Imes Avatar answered Feb 16 '23 00:02

Anderson Imes