<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. Qualcomm_Mobile_OpenCL.pdf 翻译-10-总结

    这篇文档主要是介绍了关于在Adreno GPUs上优化OpenCL代码的详细方法.文档中提供的大量信息能够帮助开发者理解OpenCL基础和Adreno结构,还有最重要的,掌握OpenCL优化技能. O ...

  2. [StructLayout(LayoutKind.Sequential) ] 是什么意思

    首先介绍一下 结构体和类的区别 :类是按引用传递 结构体是按值传递进入正题:结构体是由若干成员组成的.布局有两种1.Sequential,顺序布局,比如struct S1{int a;int b;}那 ...

  3. C Make a Square Educational Codeforces Round 42 (Rated for Div. 2) (暴力枚举,字符串匹配)

    C. Make a Square time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...

  4. 利用transform skewX制作平行四边形导航菜单

    平行四边形其实是矩形的超集:它的各条边是两两平行的,但各个角则不一定都是直角.在视觉设计中,平行四边形往往可以传达出一种动感.让我们试着用 CSS 创建一个按钮状的平行四边形链接.我们的起点就是一个普 ...

  5. git提示Please enter a commit message to explain why this merge is necessary

    Please enter a commit message to explain why this merge is necessary. 请输入提交消息来解释为什么这种合并是必要的(提交信息) gi ...

  6. Linux系统中的硬件问题如何排查?(3)

    Linux系统中的硬件问题如何排查?(3) 2013-03-27 10:32 核子可乐译 51CTO.com 字号:T | T 在Linux系统中,对于硬件故障问题的排查可能是计算机管理领域最棘手的工 ...

  7. 机器学习降维--PCA

    1.原理和概念 PCA(Principal Component Analysis),即主成分分析方法,是一种使用最广泛的数据降维算法. PCA的主要思想是将n维特征映射到k维上,这k维是全新的正交特征 ...

  8. springCloud——Eureka、Ribbon理解

    一. 服务注册中心.服务提供者.服务消费者 如何通信? 客户端: 应用主类中配置@EnableDiscoveryClient application.properties中配置defaultZone指 ...

  9. zabbix 磁盘自动发现脚本

    ##需要在zabbix界面配置宏变量===>正则来匹配磁盘 disk_discovery.sh ———————————————————————————————————————————————— ...

  10. select 语句关键字优先级

    1.select 语句优先级 select classid, userid, name, age from study where userid > 15 group by classid ha ...