Christian Ruppert
Christian Ruppert

Reputation: 3779

PRISM RegionManager - Non-XAML creation of ItemsControl RegionManager

I have a small problem with Regions in PRISM. All basics tests work fine, but now I want to replace the following XAML with pure C#:

<UserControl x:Class="CAL.Modules.Simple.Region_Testing.RegionManagerTypes.XAML.ItemsControlRegionAdapterTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation"
    Height="Auto" Width="Auto">
        <ItemsControl cal:RegionManager.RegionName="ItemsControlRegionAdapterTestRegion"/>
</UserControl>

The code inside my testing class is fairly simple, I access the RegionManager and add some test views. However, as you see in the XAML above, there is actual nothing happening in the UserControl except from attaching the RegionManager to the Control. I am sure this must be possible in Code, extending the following lines I already have:

        // MISSING
        // Creating the UserControl in CODE instead of XAML


        // Create the UserControl and add it to the main window
        regionManager.AddToRegion(RegionNames.MainRegion, new ItemsControlRegionAdapterTest());

        // Add some views to the region inside the user control
        var currentTestRegionName = TestingRegionNames.ItemsControlRegionAdapterTestRegion;
        regionManager.Regions[currentTestRegionName].Add(new BlueView());
        regionManager.Regions[currentTestRegionName].Add(new RedView());

Thanks for any tips...

Upvotes: 0

Views: 4245

Answers (2)

Christian Ruppert
Christian Ruppert

Reputation: 3779

Ok, the XamlReader Approach is working (little corrections, see source code attached)...

But honestly, it looks a little bit ugly :-) So if anybody knows how to "attach the regionManager in Code", details are welcome. First, the working XAML reader lines are:

        // MISSING
        // Creating the UserControl in CODE instead of XAML
        var obj = (UserControl)XamlReader.Parse(@"<UserControl xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                                                               xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:cal=""clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation""
Height=""Auto"" Width=""Auto"">
    <ItemsControl cal:RegionManager.RegionName=""ItemsControlRegionAdapterTestRegion""/></UserControl>");

        // Create the UserControl and add it to the main window
        regionManager.AddToRegion(RegionNames.MainRegion, obj);

GOT IT!! (at least it is working, not sure if best practice)

        var uC = new UserControl();
        var iC = new ItemsControl();
        uC.Content = iC;

        RegionManager.SetRegionName(iC, "ItemsControlRegionAdapterTestRegion");

        regionManager.AddToRegion(RegionNames.MainRegion, uC);

Thanks for all help... Comments still welcome...

Upvotes: 1

Kenan E. K.
Kenan E. K.

Reputation: 14111

Try the XamlReader approach:

private const string xaml = @"
<UserControl x:Class=""CAL.Modules.Simple.Region_Testing.RegionManagerTypes.XAML.ItemsControlRegionAdapterTest""
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    xmlns:cal=""clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation""
    Height=""Auto"" Width=""Auto"">
        <ItemsControl cal:RegionManager.RegionName=""ItemsControlRegionAdapterTestRegion""/>
</UserControl>";

//in some method
public void RunMethod()
{
     //create object and cast it
     ItemsControlRegionAdapterTest obj = (ItemsControlRegionAdapterTest) XamlReader.Parse(xaml);
}

Upvotes: 0

Related Questions