使用 ContentControl 在 WPF 中显示视图不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27577309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using ContentControl to display views in WPF doesn't work
提问by Choub890
I am currently working on a small project by myself to learn WPF better, and a question I was asking myself is how would navigation between different views work if I want to follow the MVVM pattern.
我目前正在自己做一个小项目以更好地学习 WPF,我问自己的一个问题是,如果我想遵循 MVVM 模式,不同视图之间的导航将如何工作。
I have stumbled upon this question here answering my question, but unfortunately it does not seem to work. Instead of having my view display, it is only the namespace of the associated viewmodel that is displayed. So, for exemple, instead of seeing my LoginView, I am seeing a white screen with "JollyFinance.ViewModels.LoginViewModel" written.
我在这里偶然发现了这个问题来回答我的问题,但不幸的是它似乎不起作用。没有显示我的视图,它只显示关联视图模型的命名空间。因此,例如,我看到的不是我的 LoginView,而是写有“JollyFinance.ViewModels.LoginViewModel”的白屏。
Here is my MainWindow.xaml code:
这是我的 MainWindow.xaml 代码:
<Window.Resources>
<!-- Different pages -->
<DataTemplate x:Key="LoginView" DataType="vm:LoginViewModel">
<views:LoginView />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
<Grid>
<ContentControl Content="{Binding CurrentViewModel}"/>
</Grid>
Here is my MainViewModel.cs:
这是我的 MainViewModel.cs:
public class MainViewModel : BindableObject
{
private ViewModelNavigationBase _currentViewModel;
public MainViewModel()
{
CurrentViewModel = new LoginViewModel();
}
public ViewModelNavigationBase CurrentViewModel
{
get { return _currentViewModel; }
set
{
if (_currentViewModel != value)
{
_currentViewModel = value;
RaisePropertyChanged();
}
}
}
}
My LoginViewModel.cs file:
我的 LoginViewModel.cs 文件:
public LoginViewModel()
{
LoginCommand = new RelayCommand(Login);
NavigateCommand = new RelayCommand(Login);
}
private void Login(object param)
{
Popup codePopup = new Popup();
TextBlock popupText = new TextBlock();
popupText.Text = "Test";
popupText.Background = Brushes.LightBlue;
popupText.Foreground = Brushes.Blue;
codePopup.Child = popupText;
}
public String Username { get; set; }
public String Password { get; set; }
public ICommand LoginCommand { get; set; }
}
My LoginView.xaml file is a UserControlcomprised of a few Buttonsand TextBlocks. BindableObjectis just an abstract class that I use that implements the INotifyPropertyinterface. And ViewModelNavigationBaseis the class that all my viewmodels will inherit from; it doesn't contain anything for the moment.
我的 LoginView.xaml 文件UserControl由几个Buttons和TextBlocks. BindableObject只是我使用的一个实现INotifyProperty接口的抽象类。并且ViewModelNavigationBase是我所有视图模型都将继承的类;它暂时不包含任何内容。
How would I resolve this issue for it to display my view instead of a string representation of the associated viewmodel?
我将如何解决此问题以显示我的视图而不是关联视图模型的字符串表示?
回答by nemesv
WPF does not support implicit DataTemplates with stringas the type identifier.
WPF 不支持隐式 DataTemplatesstring作为类型标识符。
You need to use the x:TypeMarkup Extensionto specify your view model type:
您需要使用x:Type标记扩展来指定您的视图模型类型:
<DataTemplate DataType="{x:Type vm:LoginViewModel}">
<views:LoginView />
</DataTemplate>
回答by Choub890
I have finally found what the problem was. For some reason, when you declare x:Key on the DataTemplate, it made the ContentControl Content appear as a string. I have removed the x:Key of the DataTemplate and everything works now. I also had to apply nemesv's answer for it to work.
我终于找到了问题所在。出于某种原因,当您在 DataTemplate 上声明 x:Key 时,它使 ContentControl 内容显示为字符串。我已经删除了 DataTemplate 的 x:Key,现在一切正常。我还必须应用 nemesv 的答案才能使其正常工作。
Before:
前:
<DataTemplate x:Key="LoginView" DataType="vm:LoginViewModel">
After (resolves the problem):
之后(解决问题):
<DataTemplate DataType="{x:Type vm:LoginViewModel}">

