C# 用户控件用作数据模板时不呈现?

C# 用户控件用作数据模板时不呈现?,c#,wpf,user-controls,datatemplate,C#,Wpf,User Controls,Datatemplate,我有一个usercontrol,我想在列表框中将其用作数据模板 这项工作: <ListBox> <ListBox.ItemTemplate> <DataTemplate> <Grid x:Name="Grid" Height="100" Width="880" Background="LightGray"> <Grid.RowDefinitions> <RowDefinition H

我有一个usercontrol,我想在列表框中将其用作数据模板

这项工作:

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
<Grid x:Name="Grid" Height="100" Width="880" Background="LightGray">
    <Grid.RowDefinitions>
        <RowDefinition Height="24"/>
        <RowDefinition Height="24"/>
        <RowDefinition Height="24"/>
        <RowDefinition Height="24"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="190" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="190" />
        <ColumnDefinition Width="200" />
    </Grid.ColumnDefinitions>
    <Label Grid.Column="0" Grid.Row="0">Client</Label>
    <Label Grid.Column="0" Grid.Row="2">Contact</Label>
    <Label Grid.Column="1" Grid.Row="0">Date Presentation</Label>
    <Label Grid.Column="2" Grid.Row="0">Action</Label>
    <Label Grid.Column="3" Grid.Row="0">Date Interview</Label>
    <Label Grid.Column="3" Grid.Row="2">Time Interview</Label>
    <Label Grid.Column="4" Grid.Row="0">Remarks</Label>
    <Label Grid.Column="5" Margin="0,0,2,0">managed by</Label>
    <ComboBox Grid.Column="0" Grid.Row="1" Margin="2" Text="{Binding Path=Customer}">
        <!--Template-->
    </ComboBox>
    <TextBox Grid.Column="0" Grid.Row="3" Margin="2" Text="{Binding Path=Contact}"></TextBox>
    <TextBox Grid.Column="1" Grid.Row="1" Margin="2" Text="{Binding Path=PresentationDate}"></TextBox>
    <ComboBox Grid.Column="2" Grid.Row="1" Margin="2" Text="{Binding Path=Action}">
        <!--Template-->
    </ComboBox>
    <TextBox Grid.Column="3" Grid.Row="1" Margin="2" Text="{Binding Path=InterviewDate}"></TextBox>
    <TextBox Grid.Column="3" Grid.Row="3" Margin="2" Text="{Binding Path=InterviewTime}"></TextBox>
    <TextBox Grid.Column="4" Grid.Row="1" Grid.RowSpan="3" Margin="2" Text="{Binding Path=Remarks}"></TextBox>
    <StackPanel Orientation="Horizontal" Grid.Column="5" Grid.Row="1" >
        <ComboBox Width="124" Text="{Binding Path=Manager}" Margin="2"></ComboBox>
        <Button Width="60" Height="20" Margin="4,0,0,0" >Mail</Button>
    </StackPanel>
    <CheckBox Grid.Column="5" Grid.Row="3" Margin="2,2,4,2">Rejection communicated</CheckBox>
   </Grid>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

客户
接触
日期介绍
行动
日期面谈
时间访谈
评论
管理人
邮寄
拒绝通知
如果我将完全相同的代码放在
标记之间:


客户
接触
日期介绍
行动
日期面谈
时间访谈
评论
管理人
邮寄
拒绝通知
进入名为CandidatePresentationControl的usercontrol,然后按如下方式执行

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <vw:CandidatePresentationControl/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

它不会被渲染。没有错误,只是一个空列表框。有人能帮我吗

谢谢大家!


编辑:我忘了一些东西,不知道是否重要:我在其中做这件事的整个过程也是一个usercontrol。

不要紧,你引用的usercontrol在另一个usercontrol中。尝试以下步骤以更好地调试XAML代码:

由于您的数据是用XAML硬连接的,因此解释空列表框的唯一方法是,父用户控件imo找不到您的用户控件。


<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <vw:CandidatePresentationControl DataContext="{Binding}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

为了绑定datacontext,您必须以这种方式编写,我建议您看看MVVM,它将让您了解如何以更好的方式进行操作。

您的用户控件XAML在哪里?为了更好的可读性,我将示例简化了一点。它基本上是上面第二个代码框中的内容,加上一个网格和一些更多的文本框和标签。当你在调试模式下运行应用程序时,VS的输出窗口中有任何信息吗?输出窗口中没有数据错误,已经检查过了。感谢你的链接!我现在可以看到更多的细节(还有一些我以前没有见过的错误),即使有了这个扩展的错误输出,使用UserControl作为DataTemplate也没有错误=(我已经将ListBox项Source绑定到该ListBox中我想要的PresentationViewModels的IEnumerable列表,从ContactViewModel绑定到整个UserControl。在资源文件中,我将CandidatePresentationControl定义为此PresentationViewModel的数据模板。
<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <vw:CandidatePresentationControl DataContext="{Binding}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>