1

如何在 WPF 的数据网格中添加删除列,当我添加此列时,在使用 ItemsSource 错误之前,得到的 Items 集合必须为空

 <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="dgStatus" 
                  HorizontalAlignment="Left" Margin="10,23,0,0" VerticalAlignment="Top" 
                  RenderTransformOrigin="-23.633,-5.198" Height="364" Width="811" 
                  CellEditEnding="myGrid_CellEditEnding" >
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Delete" x:Name="btnDelete" Click="btnDelete_Click"></Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid>
4

1 回答 1

2

你错过了<DataGrid.Columns>标签

<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="dgStatus" 
          HorizontalAlignment="Left" Margin="10,23,0,0" VerticalAlignment="Top" 
          RenderTransformOrigin="-23.633,-5.198" Height="364" Width="811" 
          CellEditEnding="myGrid_CellEditEnding" >

    <DataGrid.Columns>

            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete" x:Name="btnDelete" Click="btnDelete_Click"></Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
           </DataGridTemplateColumn>

    </DataGrid.Columns>

</DataGrid>
于 2013-08-07T10:30:59.330 回答