【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时,会发现这样一个问题:数据源变化时,虽然控件中的内容会跟着 ...
随机推荐
- 001-cut 的用法
[root@zabbix ~]# , /etc/passwd root: bin: daemon: adm: shutdown: halt: mail: operator: games: nobody ...
- 23_1spring基础
1.spring是什么 2.spring的优势 3.spring体系结构 4.耦合 程序间的依赖关系:类之间的依赖和方法之间的依赖. 解构:降低程序间的依赖关系. 实际开发中应该做到:编译期不依赖,实 ...
- 安装BCG界面库 会导致vs2013qt库配置消失
安装BCG界面库 会导致vs2013qt库配置消失 安装BCG界面库 会导致vs2013qt库配置消失 安装BCG界面库 会导致vs2013qt库配置消失
- zlib的压缩与解压
http://zlibnet.codeplex.com/releases/view/629717 using ZLibNet; string str = "ccc"; byte [ ...
- 从springmvc 过度到 springboot
1.web.xml 形式的MVC项目 web.xml ,web.xml的启动类在 web 容器里,即Tomcat中. Tomcat的main方法会加载 web.xml.为什么Tomcat会加载 web ...
- scrapy五大核心组件和中间件以及UA池和代理池
五大核心组件的工作流程 引擎(Scrapy) 用来处理整个系统的数据流处理, 触发事务(框架核心) 调度器(Scheduler) 用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. ...
- python 字符词串和字符串的转换
type(' i am ') str type(''.join('i am')) str ''.join('sda sadaa') 'sda sadaa' q=str('i am the teache ...
- 为什么要用setTimeout模拟setInterval ?
setInterval有两个缺点: 使用setInterval时,某些间隔会被跳过: 可能多个定时器会连续执行: 在前一个定时器执行完前,不会向队列插入新的定时器(解决缺点一) 保证定时器间隔(解决缺 ...
- python3.5-tensorflow-keras 安装
cpu centos FROM centos:7 MAINTAINER yon RUN yum -y install make wget \ && wget -O /etc/yum.r ...
- PWM 定义
简单的说,比如你有5V电源,要控制一台灯的亮度,有一个传统办法,就是串联一个可调电阻,改变电阻,灯的亮度就会改变.还有一个办法,就是PWM调节.不用串联电阻,而是串联一个开关.假设在1秒内,有0.5秒 ...