Prism: stack control in the region?

My Prism should insert buttons from several modules into the Shell area. The buttons will be arranged vertically, like the navigation buttons (Mail, Calendar, etc.) in Outlook 2010. I use custom controls as buttons, so I don’t have to worry about templates - my problem is the same as if I inserted simple radio buttons.

How to adjust the area so that the buttons are displayed in a vertical position? Thank you for your help.

+4
source share
6 answers

The StackPanel immediately catches your eye when you think of stacking items vertically. Unfortunately, Prism does not support StackPanel as a region out of the box. Fortunately, 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 to OverrideRegionAdapterMappings Override in your boot file.

 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 a John Papa link from which I originally received the code. (In C #, if this is what you are using) Fill in the area of ​​my prism, please

+6
source

Matt's solution is described in the Microsoft Prism Developer's Guide (V4) on pages 189-191.

For C # developers studying this issue, here is the translation of the Matt adapter into C #:

 using System.Collections.Specialized; using System.Windows; using System.Windows.Controls; using Microsoft.Practices.Prism.Regions; namespace FsNoteMaster3.Shell.Views.Utility { /// <summary> /// Enables use of a StackPanel in a Prism region. /// </summary> /// <remarks> See stackoverflow.com/questions/4950464/prism-stacking-controls-in-a-region</remarks> public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel> { /// <summary> /// Default constructor. /// </summary> /// <param name="behaviorFactory">Allows the registration of the default set of RegionBehaviors.</param> public StackPanelRegionAdapter(IRegionBehaviorFactory behaviorFactory) : base(behaviorFactory) { } /// <summary> /// Adapts a ContentControl to an IRegion. /// </summary> /// <param name="region">The new region being used.</param> /// <param name="regionTarget">The object to adapt.</param> protected override void Adapt(IRegion region, StackPanel regionTarget) { region.Views.CollectionChanged += (sender, e) => { switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (FrameworkElement element in e.NewItems) { regionTarget.Children.Add(element); } break; case NotifyCollectionChangedAction.Remove: foreach (UIElement elementLoopVariable in e.OldItems) { var element = elementLoopVariable; if (regionTarget.Children.Contains(element)) { regionTarget.Children.Remove(element); } } break; } }; } /// <summary> /// Template method to create a new instance of IRegion that will be used to adapt the object. /// </summary> /// <returns>A new instance of IRegion.</returns> protected override Microsoft.Practices.Prism.Regions.IRegion CreateRegion() { return new AllActiveRegion(); } } } 

And for Bootstrapper, this is an override of ConfigureRegionAdapterMappings () in C #, updated for Prism 4:

 /// <summary> /// Configures the default region adapter mappings to use in the application. /// </summary> /// <returns>The RegionAdapterMappings instance containing all the mappings.</returns> protected override RegionAdapterMappings ConfigureRegionAdapterMappings() { var mappings = base.ConfigureRegionAdapterMappings(); mappings.RegisterMapping(typeof(StackPanel), ServiceLocator.Current.GetInstance<StackPanelRegionAdapter>()); return mappings; } 
+6
source

From NVenhola's comment on Fill My Prism, please, in the article , there is an easy, adaptable solution:

 <ItemsControl cal:RegionManager.RegionName="XXRegionNameXX"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> 
+6
source

Why not just use the ItemsControl as a region? It stacks the elements vertically, and Prism has built-in adapters for this region. I don’t understand why you want to use the StackPanel for anything but a layout.

The ItemsControl by default uses an ItemPanel that contains a StackPanel, so it is equivalent to the answers already provided, but without unnecessary code.

+5
source

How about just using controls that just do what you want. By default, an ItemsControl will use the stack stack to display module views.

+1
source

also very careful with the RegionAdapterMapping instance in Bootstrapper.cs: write like this:

 protected override RegionAdapterMappings ConfigureRegionAdapterMappings() { //this is the correct way RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings(); regionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>()); return regionAdapterMappings; } 

It took me a few hours to realize that you cannot write:

 //RegionAdapterMappings regionAdapterMappings = new RegionAdapterMappings(); 
0
source

Source: https://habr.com/ru/post/1339089/


All Articles