我正在尝试实现一个中间类ViewModelSelector
,它设置并选择当前的View / ViewModel作为主视图主窗口MainView.xaml
的一部分显示。我试图通过在ViewModel1
内为MainView.xaml
(见下文)定义DataTemplate然后使用ContentControl
来绑定CurrentViewModel
ViewModelSelector
来实现此目的。 }}。 ViewModelSelector
将ViewModel1
分配给其属性CurrentViewModel
。然后,我们的想法是使用更多DataTemplates和ViewModel扩展它,并使用UserControl来设置ViewModelSelector
并决定显示哪个ViewModel(ViewModel1
,ViewModel2
等)。但由于某些原因,这不起作用:
使用ViewModelSelector.CurrentViewModel
将ContentControl绑定到<ContentControl Content="{Binding ViewModelSelector.CurrentViewModel}"/>
时,不显示datatemplate(请参阅下面的MainView.xaml
)。但是没有抛出其他错误(我可以说)。
出于调试目的,我还在CurrentViewModelInMainViewModel
中创建了MainViewModel.cs
属性,我将其设置为ViewModelSelector.CurrentViewModel
(CurrentViewModelInMainViewModel = ViewModelSelector.CurrentViewModel;
)。直接绑定到它(<ContentControl Content="{Binding CurrentViewModelInMainViewModel}"/>
),并显示DataTemplate。
那么我做错了什么?
以下是省略的代码。我希望我没有在其中添加任何错误,因为我现在不在工作而无法测试......
我的MainViewModel.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProgramEditor.ViewModel
{
using GalaSoft.MvvmLight;
using ProgramEditor.View;
class MainWindowViewModel : ViewModelBase
{
private ViewModelSelector ViewModelSelector;
public ViewModelSelector ViewModelSelector
{
get { return ViewModelSelector; }
set {
ViewModelSelector = value;
RaisePropertyChanged("ViewModelSelector");
}
}
private ViewModelBase currentViewModelInMainViewModel;
public ViewModelBase CurrentViewModelInMainViewModel
{
get
{
return currentViewModelInMainViewModel;
}
set
{
if (currentViewModel == value)
return;
currentViewModelInMainViewModel = value;
RaisePropertyChanged("CurrentViewModelInMainViewModel");
}
}
public MainWindowViewModel()
{
ViewModelSelector ViewModelSelector = new ViewModelSelector();
CurrentViewModelInMainViewModel = ViewModelSelector.CurrentViewModel;
}
}
}
班级ViewModelSelector
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProgramEditor.ViewModel
{
using GalaSoft.MvvmLight;
class ViewModelSelector : ViewModelBase
{
public ViewModelBase CurrentViewModel
{
get { return currentViewModel; }
set
{
if (currentViewModel == value)
return;
currentViewModel = value;
RaisePropertyChanged("CurrentViewModel");
}
}
private ViewModelBase currentViewModel;
public ViewModelSelector()
{
CurrentViewModel = new ViewModel1();
}
}
}
虚拟ViewModel1
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProgramEditor.ViewModel
{
using GalaSoft.MvvmLight;
public class ViewModel1 : ViewModelBase
{
}
}
<DataTemplate DataType="{x:Type ViewModel:FirstViewModel}">
<TextBlock Text="There be dragons here." FontSize="50"/>
</DataTemplate>
我的MainView.xaml
:
<Window x:Class="ProgramEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:ProgramEditor.View"
xmlns:ViewModel="clr-namespace:ProgramEditor.ViewModel"
Title="MainWindow" Height="900" Width="1600">
<Window.Resources>
<DataTemplate DataType="{x:Type ViewModel:ViewModel1}">
<TextBlock Text="There be dragons here." FontSize="50"/>
</DataTemplate>
</Window.Resources>
<StackPanel>
<!-- This works (shows up in main window): -->
<ContentControl Content="{Binding CurrentViewModelInMainViewModel}"/>
<!-- This does not work (does not show up in main window): -->
<!--<ContentControl Content="{Binding ViewModelSelector.CurrentViewModel}"/>-->
</StackPanel>
</Window>
答案 0 :(得分:0)
答案是一个微不足道的编程错误。我在ViewModelSelector
的构造函数中覆盖了MainViewModel.cs
属性:ViewModelSelector ViewModelSelector = new ViewModelSelector();
将该行更改为ViewModelSelector = new ViewModelSelector();
后,现在可以按预期工作。