我正在尝试将View绑定到ContentControl。目前,它只显示我的类型(例如NameSpace.ViewModel.MainWindowViewModel)
虽然我会指出,但我不确定我是否正确接近这个。
我的简单设置是我有一个View(UserControl),除了单个控件(仅为视觉放置)之外它是空的。
我的MainWindow.xaml
<Window x:Class="DelegateGoodExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="clr-namespace:DelegateGoodExample.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewModel:MainWindowViewModel x:Key="Vm" />
</Window.Resources>
<Grid>
<ContentControl Height="147" Margin="53,132,60,0"
VerticalAlignment="Top"
Content="{StaticResource Vm}" />
</Grid>
</Window>
(后面的代码中没有任何内容)。
我的MainWindowViewModel.cs
namespace DelegateGoodExample.ViewModel
{
public class MainWindowViewModel
{
private object _currentView;
public object CurrentView
{
get { return new View.QuickView(); }
set { _currentView = value; }
}
}
}
所以,我的问题是,
答案 0 :(得分:0)
您将 viewmodel 放在ContentControl
内,而不是视图中。由于您的viewmodel类不是UIElement
,并且没有DataTemplate
来确定应该如何呈现它,因此显示的只是其.ToString()
表示。
立即修复:
<ContentControl Height="147" Margin="53,132,60,0"
VerticalAlignment="Top"
Content="{Binding Source={StaticResource Vm}, Path=View}" />
但是,您应该将视图直接放在Grid
中,而不是以这种方式进行操作,并且视图模型不应该对视图有任何了解。