我猜你有的ViewModels和视图定义,所以我提出的解决方案,而不是TreeView控件我使用列表框, 在主窗口,
<Grid>
<Grid.Resources>
<DataTemplate DataType="{x:Type my:AdvancedViewModel}">
<view:AdvancedView/>
</DataTemplate>
<DataTemplate DataType="{x:Type my:RegularViewModel}">
<view:RegularView/>
</DataTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding ViewModels}" SelectedItem="{Binding SelectedViewModel}" Grid.Column="0"/>
<ContentControl Content="{Binding SelectedViewModel}" Grid.Column="1"/>
</Grid>
在这里,我使用的一些看法和视图模型例如目的,
您的看法, 首先查看
<UserControl x:Class="WpfApplication5.RegularView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication5"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="Fom Regular"/>
</Grid>
第二种观点
<UserControl x:Class="WpfApplication5.AdvancedView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication5"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="From Advanced"/>
</Grid>
,你MainViewModel只能是这样,
public class MainViewModel
{
private ObservableCollection<ViewModelBase> viewModels;
public ObservableCollection<ViewModelBase> ViewModels
{
get { return viewModels; }
set { viewModels = value; }
}
private ViewModelBase selectedViewModel;
public ViewModelBase SelectedViewModel
{
get { return selectedViewModel; }
set { selectedViewModel = value; }
}
public MainViewModel()
{
ViewModels = new ObservableCollection<ViewModelBase>();
ViewModels.Add(new RegularViewModel());
ViewModels.Add(new AdvancedViewModel());
SelectedViewModel = ViewModels[0];
}
}
public class ViewModelBase
{
}
public class RegularViewModel : ViewModelBase
{
}
public class AdvancedViewModel : ViewModelBase
{
}
谢谢你的快速响应。好吧,你创建一个新的用户控件,而不是一个页面。这不是我正在寻找的,但我现在就试一试。 – Kleisophabo
这是一个概念,你可以试试你想要的控件。 – WPFUser
现在我没有理解这个概念!以及我正在寻找的。谢谢你的回答:D。我对常规观点和高级观点感到困惑。我现在完成了作业。 :) – Kleisophabo