<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. GitHub源码攻击事件

    黑客擦除了微软多达392个代码存储库,并提出勒索要求.此前,黑客攻击了包含微软在内的大批受害者的Git存储库,删除了所有源代码和最近提交的内容,并留下了支持比特币支付的赎金票据. 勒索信息如下: “要 ...

  2. Linux下网络设置

    1.临时IP配置 # ifconfig eth0   192.168.110.118    netmask 255.255.255.0   gateway 192.168.110.2    up # ...

  3. Big Data(三)伪分布式和完全分布式的搭建

    关于伪分布式的配置全程 伪分布式图示 1.安装VMWare WorkStation,直接下一步,输入激活码即可安装 2.安装Linux(需要100GB) 引导分区Boot200MB 交换分区Swap2 ...

  4. 实现MD5算法

    using System; using System.Text; using System.Security.Cryptography; namespace Common { /// <summ ...

  5. imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable

    错误: imagecreatefromstring(): Empty string or invalid image 或者 imagesx() expects parameter 1 to be re ...

  6. 针对vs2017 对mvc 网站建设的调用问题(调用的目标发生异常)

    上篇提到,vs 2017 在mvc 网站建设中,启动登录页面实现登陆功能时,会出现调用的目标发生异常的问题,目前解决该问题的办法还没找到,本人也是vs 2017 的初学者,希望大神可以指点迷津.目前测 ...

  7. 关于 ant Checkbox.Group 数组checked 设置失效问题

    最近在频繁使用ant UI框架.在使用到checkbox的时候,需要从后台获取数组显示,然后发现数组设置了checked:true,并不能使多选框处于选中状态,阅读 Checkbox Group 的属 ...

  8. 【NOIP2016提高A组模拟8.17】(雅礼联考day1)总结

    考的还ok,暴力分很多,但有点意外的错误. 第一题找规律的题目,推了好久.100分 第二题dp,没想到. 第三题树状数组.比赛上打了个分段,准备拿60分,因为时间不够,没有对拍,其中有分段的20分莫名 ...

  9. Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)

    E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  10. linux文件夹目录含义及用途

    /boot,存放linux启动文件和内核: /initrd,boot loader initialized RAM disk,就是由boot loader初始化的内存盘.在linux内核启动前,boo ...