<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="" Height="" AutoGenerateColumns="False" GridLinesVisibility="None" CanUserDeleteRows="False"
CanUserAddRows="False" ItemsSource="{Binding ListStudent}">
<DataGridTextColumn Header="编号" Width="" Binding="{Binding Student.studentid}"/>
<DataGridTextColumn Header="姓名" Width="" Binding="{Binding Student.name}"/>
<DataGridTextColumn Header="年龄" Width="" Binding="{Binding Student.age}"/>
<DataGridTextColumn Header="性别" Width="" Binding="{Binding Student.sex}"/>
<DataGridTextColumn Header="特点" Width="" Binding="{Binding Student.remark}"/>
<DataGridTemplateColumn Header="选中" SortMemberPath="IsSelected" Width="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center" Command="{Binding Path=DataContext.SelectStudentItemCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>

报错“在使用 ItemsSource 之前,项集合必须为空”

观察一下便可发现设计器里没有正确显示各列,是因为少了DataGrid.Columns节点

 <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="" Height="" AutoGenerateColumns="False" GridLinesVisibility="None" CanUserDeleteRows="False"
CanUserAddRows="False" ItemsSource="{Binding ListStudent}">
<DataGrid.Columns>
<DataGridTextColumn Header="编号" Width="" Binding="{Binding Student.studentid}"/>
<DataGridTextColumn Header="姓名" Width="" Binding="{Binding Student.name}"/>
<DataGridTextColumn Header="年龄" Width="" Binding="{Binding Student.age}"/>
<DataGridTextColumn Header="性别" Width="" Binding="{Binding Student.sex}"/>
<DataGridTextColumn Header="特点" Width="" Binding="{Binding Student.remark}"/>
<DataGridTemplateColumn Header="选中" SortMemberPath="IsSelected" Width="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center" Command="{Binding Path=DataContext.SelectStudentItemCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

改为以上即可

【WPF异常】在使用 ItemsSource 之前,项集合必须为空的更多相关文章

  1. WPF 在使用 ItemsSource 之前,项集合必须为空

    原文:WPF 在使用 ItemsSource 之前,项集合必须为空 <DataGrid x:Name="datagrid" ItemsSource="{Bindin ...

  2. MVVM 在使用 ItemsSource 之前,项集合必须为空

    今天在做ListBox和Combobox绑定的时候,都出现过“在使用 ItemsSource 之前,项集合必须为空”的错误. Combobox比较简单,代码如下: <ComboBox x:Nam ...

  3. 判断list集合不为空

    在java开发中新手容易将判断一个list集合是否为空,只以If(list!=null)去判断,且容易和isEmpty()混淆,但是,list集合为空还是为null,是有区别的. 先看一下下面的例子, ...

  4. ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决【转】

    编写Winform程序,遇到comboBox的绑定事件和索引项变更事件的冲突问题,就是“设置 DataSource 属性后无法修改项集合”的错误问题,网上查了很多,大多说在索引项变更是进行非空判断,还 ...

  5. C# LIstbox 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题

    解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题 分类: winform2008-05-24 02:33 2592人阅读 评论(11) 收藏 举报 winf ...

  6. 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”

    解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合” 最近更新: 2013-2-15    587   很少写WinForm程序第一次使用ListBox控件就遇到了比 ...

  7. WPF异常捕获,并使程序不崩溃!

    原文:WPF异常捕获,并使程序不崩溃! 在.NET中,我们使用try-catch-finally来处理异常.但,当一个Exception抛出,抛出Exception的代码又没有被try包围时,程序就崩 ...

  8. WPF 设置类库项目为启动项,设置窗体跟随。

    原文:WPF 设置类库项目为启动项,设置窗体跟随. 1.添加用于启动的类Program.cs,需要一个静态的Main函数入口. using System; using System.Windows; ...

  9. 解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题

    解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题 当我们改变ListBox的ItemsSource时,会发现这样一个问题:数据源变化时,虽然控件中的内容会跟着 ...

随机推荐

  1. linux 桥接模式下 固定ip 设置

    DEVICE=eht0   #网卡名称BOOTPROTO=none #关闭自动获取  dhcp  IPADDR=192.168.0.178   #ip地址GATEWAY=192.168.0.1  DN ...

  2. 006-Zabbix agent on Zabbix server is unreachable for 5 minutes

    环境描述:        环境介绍:CentOS6.5   zabbix3.2.6(zabbix客户端与服务端在一台主机)   1.在安装完zabbix之后,添加客户端,客户端配置(zabbix_ag ...

  3. docker命令(随时补充)

    导入导出容器:https://blog.csdn.net/LEoe_/article/details/78685156 拷贝文件到容器内:docker cp 本地路径 容器长ID:容器路径

  4. P1903 奖学金题解

    众所周知,这是一道通过struct结构体进行排序的题目 思路:平常的输入.. 然后定义一个结构体grade,存放每个学生的学号.三科成绩.(也可以只存语文成绩和总分和学号) 自定义cmp函数,通过三层 ...

  5. python基础练习题7

    1.创建Person类,属性有姓名.年龄.性别,创建方法personInfo,打印这个人的信息2.创建Student类,继承Person类,属性有学院college,班级class,重写父类perso ...

  6. nfs存储服务实时同步

    一.NFS简介 NFS(Network File System)即网络文件系统,它允许网络中的计算机之间通过网络共享资源.将NFS主机分享的目录,挂载到本地客户端当中,本地NFS的客户端应用可以透明地 ...

  7. Python修炼之路-装饰器、生成器、迭代器

    装饰器 本质:是函数,用来装饰其他函数,也就是为其他函数添加附加功能. 使用情景 1.不能修改被装饰的函数的源代码:        2.不能修改被装饰的函数的调用方式. 在这两种条件下,为函数添加附加 ...

  8. [SpringBoot] 通过spring.factory文件来加载第三方的bean

        在springboot的开发过程中,我们经常需要加载一些bean,如果bean使我们自己写的类,那很好办,加个@Component注解就搞定了,然后过程启动会扫描启动类所在的包及其子包,如果我 ...

  9. UVALive 3211 : Now or later 【2-SAT】

    题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; ; struct TwoSAT { int n; vect ...

  10. vue中为computed计算属性传参遇到的问题,已解决

    首先介绍下项目背景, 需要将 dataList 中的 item.stars 属性传入 computed 返回要展示的值 部分代码如下(请不要纠结为什么这么做,数据格式确认如此): <li cla ...