[英]WPF ContentControl not showing user control (which should be bound by DataContext)
I have a UserControl which contains a ContentControl, which in turn should present a UserControl depending on the view model which is set as DataContext. 我有一个包含ContentControl的UserControl,而后者又应根据设置为DataContext的视图模型显示一个UserControl。
The XAML: XAML:
<UserControl.Resources>
<DataTemplate DataType="{x:Type pcViewModels:SystemPCViewModel}">
<controls:SystemPCControl />
</DataTemplate>
<DataTemplate DataType="{x:Type pcViewModels:ChipPCViewModel}">
<controls:ChipPCControl />
</DataTemplate>
<!-- Left out other definition, I guess you get the point -->
</UserControl.Resources>
<Grid Background="Aqua">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollViewer Background="Blue">
<ContentControl DataContext="{Binding CurrentContent}"/>
</ScrollViewer>
<StackPanel
Grid.Row="1"
Orientation="Horizontal" FlowDirection="RightToLeft">
<Button
Width="150" Height="50"
Content="Configure"
VerticalAlignment="Center"
Command="{Binding CurrentContent.ConfigureCommand}" />
</StackPanel>
</Grid>
I made the backgrounds of the Grid and ScrollViewer ugly visible just to be sure it was visible. 为了确保可见,我使Grid和ScrollViewer的背景丑陋可见。 So that's all visible, I can see that the view model is as DataContext (the button is also working fine).
这样就可见了,我可以看到视图模型就像DataContext一样(按钮也可以正常工作)。
I used a ContentControl before, and to all my knowledge exactly like this. 我以前使用过ContentControl,据我所知,完全是这样。 What am I doing wrong?
我究竟做错了什么?
You must set the Content property of your ContentControl . 您必须设置ContentControl的Content属性。 You can do either of the following:
您可以执行以下任一操作:
<ContentControl DataContext="{Binding CurrentContent}" Content="{Binding}" />
or 要么
<ContentControl Content="{Binding CurrentContent}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.