« Prism navigation with tabcontrol » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:WPF = TabControl = <filebox fn='MainWindow.xaml'> <Window x:Class="NavigationWPF.Views.MainWindow" xmlns:prism="http://prismlibrary.com/" pris… »)
 
Ligne 5 : Ligne 5 :
         xmlns:prism="http://prismlibrary.com/"
         xmlns:prism="http://prismlibrary.com/"
         prism:ViewModelLocator.AutoWireViewModel="True"
         prism:ViewModelLocator.AutoWireViewModel="True"
         Title="{Binding Title}" Height="350" Width="525">
         Title="MainWindow" Height="350" Width="525">


     <Window.Resources>
     <Window.Resources>
Ligne 38 : Ligne 38 :


<filebox fn='MainWindowViewModel.cs'>
<filebox fn='MainWindowViewModel.cs'>
public class MainWindowViewModel : ViewModelBase
public class MainWindowViewModel : BindableBase
{
{
     private readonly IRegionManager _regionManager;
     private readonly IRegionManager _regionManager;

Version du 12 octobre 2021 à 21:06

TabControl

MainWindow.xaml
<Window x:Class="NavigationWPF.Views.MainWindow"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <Style TargetType="TabItem">
            <Setter Property="Header"
                    Value="{Binding DataContext.Title}" />
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <StackPanel Orientation="Horizontal">
            <Button Command="{Binding NavigateCommand}"
                    CommandParameter="ViewA">
                Navigate A
            </Button>
            <Button Command="{Binding NavigateCommand}"
                    CommandParameter="ViewB">
                Navigate B
            </Button>
        </StackPanel>

        <TabControl Grid.Row="1"
                    prism:RegionManager.RegionName="TabRegion" />
    </Grid>
</Window>
MainWindowViewModel.cs
public class MainWindowViewModel : BindableBase
{
    private readonly IRegionManager _regionManager;

    public DelegateCommand<string> NavigateCommand { get; set; }

    public MainWindowViewModel(IRegionManager regionManager)
    {
        _regionManager = regionManager;
        NavigateCommand = new DelegateCommand<string>(Navigate);
    }

    // ViewA and ViewB are command parameters passed as navigationPath
    void Navigate(string navigationPath)
    {
        // open/reuse a tab and inject the view in it
        _regionManager.RequestNavigate("TabRegion", navigationPath);
    }
}

Navigation configuration

App.xaml.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    // register the views
    containerRegistry.RegisterForNavigation<ViewA>();
    containerRegistry.RegisterForNavigation<ViewB>();
}

View to inject

ViewA.xaml
<UserControl x:Class="NavigationWPF.Views.ViewA"
             xmlns:mvvm="http://prismlibrary.com/"
             mvvm:ViewModelLocator.AutoWireViewModel="True">
    <Grid>
        <TextBlock Text="View A"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   FontSize="48" />
    </Grid>
</UserControl>
ViewAViewModel.cs
public class ViewAViewModel : ViewModelBase
{
    public ViewAViewModel()
    {
        Title = "View A";
    }
}
ViewModelBase.cs
public class ViewModelBase : BindableBase, INavigationAware
{
    private string _title;

    public string Title
    {
        get => _title;
        set => SetProperty(ref _title, value);
    }

    public virtual bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {

    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {

    }
}