bytecode77
bytecode77

Reputation: 14860

How to implement navigation with a TreeView instead of a Tabber in WPF?

Imagine a Tabber in WPF with multiple tab pages. Now I would like to use a TreeView for navigation and not the TabPages.

The TreeView toggles between the content pages, without deleting and re-creating them.

How is this implemented?

Should there still be a Tabber with hidden TabPages or is there a different approach? What's the canonical way to achieve this?

Upvotes: 1

Views: 119

Answers (1)

Jose
Jose

Reputation: 1897

I've used this in many applications and seems to be correct and nonMVVM breaking. Maybe that is what you are looking for:

public class ShellViewModel : INotifyPropertyChanged
    {
        private ICommand _changeViewModelCommand;

        private object _currentViewModel;
        private List<object> _viewModels = new List<object>();

        public ShellViewModel()
        {
            ViewModels.Add(new HomeViewModel());
            CurrentViewModel = ViewModels[0];
        }

        private void ChangeViewModel(object viewModel)
        {
            if (!ViewModels.Contains(viewModel))
                ViewModels.Add(viewModel);

            CurrentViewModel = ViewModels.FirstOrDefault(vm => vm == viewModel);
        }
    }

And the view:

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

    <!-- Header -->
    <TextBlock Text="Application Name" FontWeight="Bold" FontSize="24" />

    <Line Grid.Row="1" Stroke="Black" Margin="0,5" StrokeThickness="1" Stretch="Fill" X2="1" />
    <!-- Content -->
    <ContentControl Grid.Row="2" Content="{Binding CurrentViewModel}"/>
</Grid>

Thanks to Rachel Lim's Blog

I know this is not using a TreeView, but maybe it will fit your necesities. Hope it helps.

Upvotes: 1

Related Questions