如何在 WPF 中使用 TreeView 而不是 Tabber 实现导航?

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

想象一下 WPF 中的 Tabber 具有多个标签页。现在我想使用 TreeView 进行导航, 而不是 TabPages。

TreeView 在内容页面之间切换,无需删除和重新创建它们。

这是如何实现的?

是否还应该有一个带有隐藏 TabPage 的 Tabber 还是有不同的方法?实现这一目标的规范方法是什么?

我已经在许多应用程序中使用过它并且似乎是正确的并且没有破坏 MVVM。也许这就是您正在寻找的:

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);
        }
    }

和视图:

<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>

感谢Rachel Lim's Blog

我知道这不是在使用 TreeView,但也许它会满足您的需要。希望对你有帮助。