在数据网格 wpf 中使用带有复选框控件的 ItemsSource 之前,项目集合必须为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18147707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:24:29  来源:igfitidea点击:

Items collection must be empty before using ItemsSource with checkbox control in datagrid wpf

c#wpfxaml

提问by Mark

This error only occurs when I add this following XAML code : enter image description here

仅当我添加以下 XAML 代码时才会发生此错误: 在此处输入图片说明

 <DataGridTemplateColumn Header="Ist aktiv" IsReadOnly="True">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <CheckBox IsChecked="{Binding IsActiveBool}"  />
          </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>

</DataGrid>

采纳答案by Sheridan

There are a number of possible reasons why you got this error, most (if not all) of which have already been discussed here on StackOverflow. Take a look at the following posts:

出现此错误的可能原因有很多,其中大部分(如果不是全部)已经在 StackOverflow 上讨论过。看看以下帖子:

Items collection must be empty before using ItemsSource in Silverlight

在 Silverlight 中使用 ItemsSource 之前,Items 集合必须为空

Error: Items collection must be empty before using ItemsSource

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

Getting an “Items collection must be empty before using ItemsSource” with EF

使用 EF 获取“在使用 ItemsSource 之前必须为空的项目集合”

DataGridTemplateColumn : Items collection must be empty before using ItemsSource.

DataGridTemplateColumn :在使用 ItemsSource 之前,Items 集合必须为空。

Items collection must be empty before using ItemsSource

在使用 ItemsSource 之前,Items 集合必须为空

If you don't find an answer in these posts, perhaps you could do your own search... it is generally preferable for SO users to search beforethey ask questions here.

如果您在这些帖子中没有找到答案,也许您可​​以自己进行搜索……SO 用户通常最好他们在这里提问之前先进行搜索。

回答by sm2mafaz

I also faced a similar problem for the first time, and I noticed you could even get this error message if you have forgot to add/enclose the

我也第一次遇到了类似的问题,我注意到如果您忘记添加/附上

<DataGrid.Columns> </DataGrid.Columns>

tags

标签

回答by Eric Ouellet

Just for information...

仅供参考...

I had the problem because I added this style which seems very strange:

我遇到了这个问题,因为我添加了这种看起来很奇怪的样式:

    <Style TargetType="{x:Type DataGridCell}">
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
    </Style>

回答by margaret

Here is my code! It works. Really, I forgot to put tags in an appropriate place.

这是我的代码!有用。真的,我忘了把标签放在合适的地方。

<DataGrid x:Name="CostsDataGrid" HorizontalAlignment="Left" Margin="307,98,0,0" VerticalAlignment="Top" Height="260" Width="313" AutoGenerateColumns="False" MinColumnWidth="35" VerticalGridLinesBrush="#FF7A7878" HorizontalGridLinesBrush="#FF7A7878" RowHeight="35" Foreground="Black" PreviewMouseRightButtonDown="CostsDataGrid_PreviewMouseRightButtonDown" >
    <DataGrid.Columns>
    <DataGridTemplateColumn Header="состояние" Width="70" IsReadOnly="False">
        <DataGridTemplateColumn.CellTemplate >
            <DataTemplate>
                <CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" Checked="OnChecked" Unchecked="OffChecked"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

        <DataGridTextColumn Header="расход" Binding="{Binding name}" Width="121" IsReadOnly="True" />
        <DataGridTextColumn Header="сумма" Binding="{Binding price}" Width="120" IsReadOnly="True" />
    </DataGrid.Columns>
</DataGrid>