繁体   English   中英

WPF ContentControl不显示用户控件(应由DataContext绑定)

[英]WPF ContentControl not showing user control (which should be bound by DataContext)

我有一个包含ContentControl的UserControl,而后者又应根据设置为DataContext的视图模型显示一个UserControl。

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>

为了确保可见,我使Grid和ScrollViewer的背景丑陋可见。 这样就可见了,我可以看到视图模型就像DataContext一样(按钮也可以正常工作)。

我以前使用过ContentControl,据我所知,完全是这样。 我究竟做错了什么?

您必须设置ContentControlContent属性。 您可以执行以下任一操作:

<ContentControl DataContext="{Binding CurrentContent}" Content="{Binding}" />

要么

<ContentControl Content="{Binding CurrentContent}" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM