wpf - 自定义用户控件未显示在 DataGrid 中

wpf - Custom UserControl not being displayed in DataGrid

我正在尝试创建一个在一行中显示自定义用户控件的数据网格。数据网格实际上只有一列,当添加新行时,该用户控件的新实例应显示在新行中。我还没有实现添加按钮,所以我暂时将它设置为加载一个已经创建的自定义用户控件。但是,什么也没有发生;唯一显示的是列 Header.

这是我的 XAML:

<Grid>
    <DataGrid ColumnWidth="*" AutoGenerateColumns="False" x:Name="dgLOA">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Pay Advice">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <local:About/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

您必须为 DataGrid 设置 ItemSource,这样自定义控件将根据 itemsource 的计数显示,

 <Grid>
        <DataGrid ColumnWidth="*" AutoGenerateColumns="False" x:Name="dgLOA">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Pay Advice">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <local:MyControl/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

您必须在代码隐藏或 xaml 文件中设置 ItemSOurce,如下所示,

dgLOA.ItemsSource = GetEmployees(); 

在Xaml

ItemsSource="{绑定员工}"