今天在做ListBox和Combobox绑定的时候,都出现过“在使用 ItemsSource 之前,项集合必须为空”的错误。

Combobox比较简单,代码如下:

  <ComboBox x:Name="cbxPage" Width="30" Height="30" BorderBrush="{StaticResource CustomBorderColor}"
Style="{StaticResource CustomCombobox}" ItemsSource="{Binding PageList}" SelectedItem="{Binding CurrentPageIndex,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="True"
>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding CbxSelectedChangeCommand}" CommandParameter="{Binding SelectedItem,RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}"/>
</i:EventTrigger>
</ComboBox>

编译没有问题,运行时报错:“在使用 ItemsSource 之前,项集合必须为空”,百思不得其解,最后挨个检查,竟然是因为使用Interaction绑定command事件的时候代码有误,修改成下面的就可以了:

  <i:Interaction.Triggers >
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding CbxSelectedChangeCommand}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>

ListBox是想要横向展示图片并且自动换行,代码如下:

 <ListBox ItemsSource="{Binding PersonList}" SelectedItem="{Binding CurrentPerson,Mode=TwoWay,NotifyOnSourceUpdated=True}"  ItemContainerStyle="{StaticResource PersonListBoxStyle}">
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
<ListBox.Items>
<StackPanel Orientation="Vertical" >
<Image Width="145" Height="145" Stretch="Fill" Source="{Binding Path=show_pic,Converter={StaticResource FacePictureConverter}}"></Image>
<Grid Width="145" Height="22">
<TextBlock Text="{Binding p_name}" FontSize="14" Foreground="{StaticResource RichImageTextForeground}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
</StackPanel>
</ListBox.Items>
</ListBox>

编译没有问题,运行时出错,因为是绑定数据源,所以不存在绑定之前Itemsource.clear(),与数据源无关。

查了很多资料,其中有一篇:http://www.verydemo.com/demo_c441_i91243.html

看了一下,觉得可能与ListBox的Template有关,试着改了一下,将ListBox.Items中的内容放到DataTemplate 中,然后使用ItemTemplate,就可以了

更改后的代码如下:

 <ListBox ItemsSource="{Binding PersonList}" SelectedItem="{Binding CurrentPerson,Mode=TwoWay,NotifyOnSourceUpdated=True}"  ItemTemplate="{DynamicResource persontemplate}" ItemContainerStyle="{StaticResource PersonListBoxStyle}">
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
<ListBox.Resources>
<DataTemplate x:Key="persontemplate">
<StackPanel Orientation="Vertical" >
<Image Width="145" Height="145" Stretch="Fill" Source="{Binding Path=show_pic,Converter={StaticResource FacePictureConverter}}"></Image>
<Grid Width="145" Height="22">
<TextBlock Text="{Binding p_name}" FontSize="14" Foreground="{StaticResource RichImageTextForeground}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.Resources>
</ListBox>

所以,深入了解,才能更好地使用,继续学习。

MVVM 在使用 ItemsSource 之前,项集合必须为空的更多相关文章

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

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

  2. 【WPF异常】在使用 ItemsSource 之前,项集合必须为空

    <DataGrid x:Name=" AutoGenerateColumns="False" GridLinesVisibility="None" ...

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

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

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

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

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

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

  6. java 对象、集合的非空判断

    自我总结,有什么不到位的地方,请各位纠正补充,感激不尽! 目的:使程序更严谨 ***对象验证是否不为空:  if( null != obj ) ***List验证不为空:if( null != lis ...

  7. 判断集合不为空Java

    list.size>0判断集合size是否大于0 list.isEmpty()判断集合是否为空,返回布尔值

  8. 判断list集合不为空

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

  9. 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空

    通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...

随机推荐

  1. linux 命令之comm

    1. 简介 comm命令可以用于两个文件之间的比较,它有一些选项可以用来调整输出,以便执行交集.求差.以及差集操作. 交集:打印出两个文件所共有的行. 求差:打印出指定文件所包含的且不相同的行. 差集 ...

  2. modelsim仿真xilinx mig ip core相关问题

    1.运用自动化脚本文件 do sim.do  其中不支持 .f文件 , 需要直接vlog 2.对于mig模型采用下面句型(根据example中do sim.do文件) vlog -sv +define ...

  3. linux crontab

    概念: Linux 工作排程的种类:at, cron at 是个可以处理仅执行一次就结束排程的指令,不过要执行 at 时, 必须要有 atd 这个服务支持. crontab 这个指令所设定的工作将会循 ...

  4. code project 上的内存管理的示例代码

    /******************************************************************** created: 2014/03/17 18:53 file ...

  5. Java重点之小白解析--浅谈数据流形式图片上载

    文档上载,上载也不知道哪个大神(混球)起的名字,读起来怪怪的,反正平时我只读上传. 闲话少说,直入主题.先等等这两天做文件上传,都快把宝宝折磨疯了,不会呀,各种查呀,最可悲的是废了老大功夫学会了传送文 ...

  6. 把vim当做golang的IDE

    开始决定丢弃鼠标,所以准备用vim了. 那么在vim里面如何搭建golang环境呢? git盛行之下,搭建vim环境是如此简单. 而且vim搭建好了之后,基本上跟IDE没有差别. 高亮.自动补全.自动 ...

  7. ueditor不自动加P解决方法

    百度的Ueditor编辑器出于安全考虑; 用户在html模式下粘贴进去的html文档会自动被去除样式和转义. 虽然安全的,但是非常不方便. 做一下修改把这个功能去掉. 一.打开ueditor.all. ...

  8. Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法

    Java中List,ArrayList.Vector,map,HashTable,HashMap区别用法 标签: vectorhashmaplistjavaiteratorinteger ArrayL ...

  9. Java在处理大数据的时候一些小技巧

    Java在处理大数据的时候一些小技巧 发布时间:2013-05-09 00:00:00 来源:中国IT实验室 作者:佚名   关键字:Java 众所周知,java在处理数据量比较大的时候,加载到内存必 ...

  10. MySQL高级查询语句

    高级查询: 一:多表连接 1.select Info.Code,Info.Name,Nation.Name from Info,Nation where Info.Nation = Nation.Co ...