【WPF异常】在使用 ItemsSource 之前,项集合必须为空
<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 之前,项集合必须为空的更多相关文章
- WPF 在使用 ItemsSource 之前,项集合必须为空
原文:WPF 在使用 ItemsSource 之前,项集合必须为空 <DataGrid x:Name="datagrid" ItemsSource="{Bindin ...
- MVVM 在使用 ItemsSource 之前,项集合必须为空
今天在做ListBox和Combobox绑定的时候,都出现过“在使用 ItemsSource 之前,项集合必须为空”的错误. Combobox比较简单,代码如下: <ComboBox x:Nam ...
- 判断list集合不为空
在java开发中新手容易将判断一个list集合是否为空,只以If(list!=null)去判断,且容易和isEmpty()混淆,但是,list集合为空还是为null,是有区别的. 先看一下下面的例子, ...
- ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决【转】
编写Winform程序,遇到comboBox的绑定事件和索引项变更事件的冲突问题,就是“设置 DataSource 属性后无法修改项集合”的错误问题,网上查了很多,大多说在索引项变更是进行非空判断,还 ...
- C# LIstbox 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题
解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题 分类: winform2008-05-24 02:33 2592人阅读 评论(11) 收藏 举报 winf ...
- 解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”
解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合” 最近更新: 2013-2-15 587 很少写WinForm程序第一次使用ListBox控件就遇到了比 ...
- WPF异常捕获,并使程序不崩溃!
原文:WPF异常捕获,并使程序不崩溃! 在.NET中,我们使用try-catch-finally来处理异常.但,当一个Exception抛出,抛出Exception的代码又没有被try包围时,程序就崩 ...
- WPF 设置类库项目为启动项,设置窗体跟随。
原文:WPF 设置类库项目为启动项,设置窗体跟随. 1.添加用于启动的类Program.cs,需要一个静态的Main函数入口. using System; using System.Windows; ...
- 解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题
解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题 当我们改变ListBox的ItemsSource时,会发现这样一个问题:数据源变化时,虽然控件中的内容会跟着 ...
随机推荐
- Delphi 指令符
- 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)
链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...
- FPDF_CHAR_INFO
typedef struct { FX_WCHAR m_Unicode; FX_WCHAR m_Charcode; FX_INT32 m_Flag; FX_FLOAT m_FontSize; FX_F ...
- zabbix_server: error while loading shared libraries: libperconaserverclient.so.18
zabbix_server: error while loading shared libraries: libperconaserverclient.so.18 [root@dba_test_002 ...
- Flask【第9篇】:Flask-script组件
flask-script组件 Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他 ...
- ZROI 19.08.10模拟赛
传送门 写在前面:为了保护正睿题目版权,这里不放题面,只写题解. A \(20pts:\) 枚举操作序列然后暴力跑,复杂度\(O(6^n)\). \([50,80]pts:\) 枚举改成dfs,每层操 ...
- 使用tensorflow训练word2vec
from http://blog.csdn.net/wangyangzhizhou/article/details/77530479?locationNum=1&fps=1 使用了tensor ...
- linq 分页
urList = (from u in urList orderby u.toolingNo_C select u) ...
- html article标签 语法
html article标签 语法 article标签有什么作用?直线电机生产厂家 作用:html中article标签的作用是规定独立的自包含内容,其中外部内容是来自一个外部的新闻提供者的一篇新文章, ...
- windows Apache ab安装及压力测试
一:安装 ab是Apache自带的网站压力测试工具.使用起来非常的简单和方便.不仅仅是可以Apache服务器进行网站访问压力测试,还可以对其他类型的服务器进行压力测试.比如nginx,tomcat,I ...