【问题标题】:How to implement navigation with a TreeView instead of a Tabber in WPF?如何在 WPF 中使用 TreeView 而不是 Tabber 实现导航?
【发布时间】:2015-09-10 12:49:52
【问题描述】:

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

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

这是如何实现的?

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

【问题讨论】:

    标签: c# wpf navigation


    【解决方案1】:

    我在许多应用程序中都使用过它,并且似乎是正确的并且非 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,但也许它会满足您的需求。希望对您有所帮助。

    【讨论】:

    • 所以ContentControl.Content 可以绑定到代码后面的UserControl 吗?
    • 刚试过。简单且正是我想要的! +1
    • 我害怕给出完全不同的解决方案。很高兴它有帮助^^
    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多