1

Using WPF .Net Core 3.1 + Prism.

I've implemented TabControl with dynamically added TabItems. RegionManager works just fine when trying to navigate to region within MainWindow, but it failed when trying to navigate to region whithin the TabPage. Does not work neither from MainWindowViewModel, nor from TabPageViewModel.

Project's repo may be found here: GitHub.

MainWindow.xaml:

 <TabControl Margin="10" ItemsSource="{Binding TabPages, UpdateSourceTrigger=PropertyChanged}">
      <TabControl.Resources>
           <DataTemplate DataType="{x:Type viewmodels:SomeTabPageViewModel}">
                <views:SomeTabPage/>
           </DataTemplate>
      </TabControl.Resources>

      <TabControl.ItemTemplate>
           <DataTemplate DataType="{x:Type interfaces:ITabPage}" x:Name="dt">
                <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="40">
                     <TextBlock Grid.Column="1" Margin="5,10,10,10" Text="{Binding TabName}" TextAlignment="Center" VerticalAlignment="Center" FontSize="16"/>
                </Grid>
           </DataTemplate>
      </TabControl.ItemTemplate>
 </TabControl>

SomeTabPage.xaml:

<UserControl x:Class="WpfPrismRegionFailure.Views.SomeTabPage"
         xmlns:prism="http://prismlibrary.com/"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Content="Navigate From Inside Of TabPage (not working)" Command="{Binding NavigateFromInsideOfTabPageCommand}" FontSize="16" Background="Orange"/>
    <TextBlock Grid.Row="1" Margin="10,20,0,0" Text="The area below contains region 'RegionInsideTabPage', but can't navigate to it." FontSize="16"/>

    <Border Grid.Row="2" BorderBrush="Orange" BorderThickness="2" CornerRadius="10" Margin="10">
        <ContentControl Margin="10" prism:RegionManager.RegionName="RegionInsideTabPage"/>
    </Border>
</Grid>
</UserControl>

SomeTabPageViewModel.cs:

using Prism.Commands;
using Prism.Ioc;
using Prism.Regions;
using System.Windows.Input;
using WpfPrismRegionFailure.Base;

namespace WpfPrismRegionFailure.ViewModels
{
    public class SomeTabPageViewModel : TabPageBase
    {
        public SomeTabPageViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            this.NavigateFromInsideOfTabPageCommand = new DelegateCommand(OnNavigateFromInsideOfTabPage);
        }
        
        public SomeTabPageViewModel(IRegionManager regionManager) : base (regionManager)
        {
            this.NavigateFromInsideOfTabPageCommand = new DelegateCommand(OnNavigateFromInsideOfTabPage);
        }

        public ICommand NavigateFromInsideOfTabPageCommand { get; }

        private void OnNavigateFromInsideOfTabPage()
        {
            System.Diagnostics.Trace.WriteLine($"SomeTabPageViewModel _regionManager ContainsRegionWithName RegionInsideTabPage: {this._regionManager.Regions.ContainsRegionWithName("RegionInsideTabPage")}");

            this._regionManager.RequestNavigate("RegionInsideTabPage", "RegionContent");
        }
    }
}

Inside of TabPageViewModel regionManager.Regions.ContainsRegionWithName("RegionInsideTabPage") returns 'false' despite of the region is being declared in XAML

<ContentControl Margin="10" prism:RegionManager.RegionName="RegionInsideTabPage"/>

Project's repo may be found here: GitHub.

Someone heeeeeeelp me please

kevintw
  • 58
  • 5
  • This may not be directly related to your issue but you should review your use of DataTemplate binding to Interfaces as it's not straightforward. See this response for an example of the requirements. [https://stackoverflow.com/a/25642988/3596482](https://stackoverflow.com/a/25642988/3596482) – bakerhillpins Dec 21 '21 at 15:33
  • I don't think it is related to DataTemplate binding to Interface as long as `TabControl.ItemTemplate` works just fine. The problem is with `TabControl.ContentTemplate`, but even if I bind it implicitly via `UserControl` property of `ITabPage`, the region does not show up anyway. The only way I managed to get it work is `this._regionManager.RegisterViewWithRegion("RegionInsideTabPage", "RegionContent");`, but it can only be called from inside of TabPage's ViewModel, not from MainWindowViewModel, and moreover, once it get called, I could't override it by any other ViewModel. – kevintw Dec 21 '21 at 16:31
  • I've read your question several times now and can't figure out what you are actually trying to navigate to? Do you want the specific tab page to become active or are you trying to navigate within a tab? In the code you are creating multiple instances of the same region and that requires scoped region managers and view injection [per](https://prismlibrary.com/docs/wpf/view-composition.html#creating-multiple-instances-of-a-region). – bakerhillpins Dec 21 '21 at 22:17
  • I want to create Tab page dynamically (and it done) and then to inject some view to the region inside of that Tab page. The region named 'RegionInsideTabPage' is inside of the Tab page and declared in XAML, but when I fire `this._regionManager.RequestNavigate("RegionInsideTabPage", "RegionContent")` nothing happening. No matter if I fire it from TabPage's ViewModel or from MainWindowViewModel, it looks like region 'RegionInsideTabPage' is absent at all. – kevintw Dec 22 '21 at 09:06

0 Answers0