How do I use RegionManager.RegionName in ControlTemplate

Topics: Prism v2 - WPF 3.5
Jun 30, 2009 at 6:45 AM

I ues the RegionManager.RegionName In ControlTemplate of Window, but a exception has throwed.

---------------------------------------------------------------
An exception occurred while initializing module 'OrderModule'.
    - The exception message was: The region manager does not contain the MainRegion region.
    - The Assembly that the module was trying to be loaded from was:Commanding.Modules.Order, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
    Check the InnerException property of the exception for more information. If the exception occurred while creating an object in a DI container, you can exception.GetRootException() to help locate the root cause of the problem.

----------------------------------------------------------------

this is xaml

<Window x:Class="Commanding.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="http://www.codeplex.com/CompositeWPF"
    Title="Shell" Height="385" Width="500" MinHeight="385">
    <Window.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFFFFFFF" Offset="0"/>
            <GradientStop Color="#FCFFF5" Offset="0.992"/>
            <GradientStop Color="#3E606F" Offset="0.185"/>
        </LinearGradientBrush>
    </Window.Background>
    <Window.Template>
        <ControlTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="1"  Margin="10,0,10,0">
                    <ItemsControl cal:RegionManager.RegionName="GlobalCommandsRegion" />
                </StackPanel>
                <Border Grid.Row="2" CornerRadius="4,4,4,4" BorderBrush="#193441" Background="#FCFFF5" BorderThickness="2,2,2,2" Margin="10,0,10,10" Padding="5">
                    <StackPanel>
                        <ItemsControl cal:RegionManager.RegionName="MainRegion" />
                    </StackPanel>
                </Border>
                <Label HorizontalAlignment="Left" Margin="155,5,0,0" Width="Auto" Content="QuickStart" FontWeight="Normal" Foreground="#FF373737" FontSize="24" FontFamily="Corbel"/>
                <Label HorizontalAlignment="Left" Margin="10,5,0,0" Width="Auto" Content="Commanding" FontWeight="Bold" Foreground="#FF373737" FontSize="24" FontFamily="Corbel"/>
                <Separator Margin="0,0,0,0" VerticalAlignment="Bottom" Height="10" BorderBrush="#193441"/>
            </Grid>
        </ControlTemplate>
    </Window.Template>
  
</Window>

Jun 30, 2009 at 7:46 AM

I think the problem is

 

 private IRegionManager FindRegionManager(DependencyObject dependencyObject)
        {
            var regionmanager = this.RegionManagerAccessor.GetRegionManager(dependencyObject);
            if (regionmanager != null)
            {
                return regionmanager;
            }

            DependencyObject parent = null;
#if SILVERLIGHT
            //parent = System.Windows.Media.VisualTreeHelper.GetParent(dependencyObject);
                    FrameworkElement frameworkElement = dependencyObject as FrameworkElement;
                    if (frameworkElement != null)
                    {
                        parent = frameworkElement.Parent;
                    }
#else
            parent = LogicalTreeHelper.GetParent(dependencyObject);
#endif
            if (parent != null)
            {
                return this.FindRegionManager(parent);
            }

            return null;
        }

 

if i use the flowing code, i don'g know what bug will happend!

 

        private IRegionManager FindRegionManager(DependencyObject dependencyObject)
        {
            var regionmanager = this.RegionManagerAccessor.GetRegionManager(dependencyObject);
            if (regionmanager != null)
            {
                return regionmanager;
            }

            DependencyObject parent = null;
#if SILVERLIGHT
            //parent = System.Windows.Media.VisualTreeHelper.GetParent(dependencyObject);
                    FrameworkElement frameworkElement = dependencyObject as FrameworkElement;
                    if (frameworkElement != null)
                    {
                        parent = frameworkElement.Parent;
                    }
#else
            parent = VisualTreeHelper.GetParent(dependencyObject);
#endif
            if (parent != null)
            {
                return this.FindRegionManager(parent);
            }

            return null;
        }

Jun 30, 2009 at 6:23 PM

Hi,

 

A couple of weeks ago, the following thread was answered that deals with a similar situation. Perhaps you can find it useful:

·         Setting RegionName with Style Setters

 

Please let me know if this helps.

 

Damian Schenkelman

http://blogs.southworks.net/dschenkelman

Jul 1, 2009 at 1:50 PM

Hi, dschenkelman

Thanks for you answer! thanks!

Mar 2, 2010 at 9:21 PM

Hi mww,

Did you find a work around for this? I have the same challenge.

 

Regards,

Hrej