简体   繁体   English

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

[英]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 . 您必须设置ContentControlContent属性。 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.

相关问题 我可以通过绑定到wpf父控件上的属性的依赖项属性在xaml中设置用户控件datacontext吗? - Can I set my user control datacontext in xaml via a dependency property bound to a property on the parent control in wpf? 另一个ContentControl中的WPF ContentControl不显示 - WPF ContentControl inside another ContentControl not showing 用户类为DataContext时使用DataTemplate和ContentControl - DataTemplate and ContentControl when user class as DataContext 将ContentControl绑定到ApplicationViewModel将确定要查看哪个用户控件? - Binding ContentControl to the ApplicationViewModel that will determine which user control to view? 具有依赖项属性WPF的用户控件DataContext /绑定问题 - User Control DataContext/Binding Issue with Dependency Property WPF 在 WPF 中的客户端应用程序中设置用户控件的数据上下文 - Setting up datacontext of the user control in client application in WPF WPF - window 的数据上下文中的命令和来自用户控件中的列表视图的命令参数 - WPF - Command in datacontext of the window and commandparameter from listview in user control 将参数传递给绑定到gridview的用户控件 - Passing parameter to a user control which is bound to a gridview 用户控件的数据上下文是什么? - What is the datacontext of the user control? 将DataContext传递给用户控件 - Passing DataContext to a User Control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM