Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中使用TreeView而不是Tabber实现导航?_C#_Wpf_Navigation - Fatal编程技术网

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

C# 如何在WPF中使用TreeView而不是Tabber实现导航?,c#,wpf,navigation,C#,Wpf,Navigation,假设WPF中有一个带有多个选项卡页的选项卡。现在,我想使用TreeView进行导航,而不是选项卡页面 TreeView在内容页之间切换,无需删除和重新创建内容页 这是如何实现的? 是否仍应该有一个带有隐藏选项卡页的选项卡,或者有不同的方法?实现这一点的标准方法是什么?我在许多应用程序中都使用了这一方法,而且似乎是正确的,而且是非破坏性的。也许这就是你想要的: public class ShellViewModel : INotifyPropertyChanged { pr

假设WPF中有一个带有多个选项卡页的
选项卡。现在,我想使用
TreeView
进行导航,而不是选项卡页面

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

这是如何实现的?


是否仍应该有一个带有隐藏选项卡页的
选项卡
,或者有不同的方法?实现这一点的标准方法是什么?

我在许多应用程序中都使用了这一方法,而且似乎是正确的,而且是非破坏性的。也许这就是你想要的:

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);
        }
    }
公共类ShellViewModel:INotifyPropertyChanged
{
私有ICommand_changeViewModelCommand;
私有对象_currentViewModel;
私有列表_viewModels=新列表();
公共ShellViewModel()
{
添加(新的HomeViewModel());
CurrentViewModel=ViewModels[0];
}
私有void ChangeViewModel(对象视图模型)
{
如果(!ViewModels.Contains(viewModel))
添加(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>

多亏了


我知道这不是用树视图,但也许它会适合你的需要。希望有帮助。

这样
ContentControl.Content
就可以绑定到代码隐藏中的
UserControl
?刚刚尝试过。简单,正是我要找的+1我害怕给出完全不同的解决方案。很高兴有帮助^^