在使用ItemsSource错误之前,项集合必须为空

时间:2013-08-07 10:26:03

标签: c# wpf

如何在WPF中的datagrid中添加删除列,当我添加此列时,在使用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>

1 个答案:

答案 0 :(得分: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>