今天在做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. JS逻辑运算符&&与||的短路运算

    最近看到一个360面试题,题目如下: 下面代码的输出值是? alert(1&&2); 正确的结果是 2. 1.后来仔细研究了一下JS逻辑运算的相关内容,在MDN上面找到相应描述: 下面 ...

  2. 数据库mysql优化方案

    1.创建索引对于查询占主要的应用来说,索引显得尤为重要.很多时候性能问题很简单的就是因为我们忘了添加索引而造成的,或者说没有添加更为有效的索引导致.如果不加索引的话,那么查找任何哪怕只是一条特定的数据 ...

  3. 触摸屏测试:Tslib

    触摸屏测试:Tslib(配置了一天,心累) 总结:网上有许多关于tslib的步骤/错误总结,所以这里只是提几个我觉得比较棘手的问题: 1,我明明给arm-linux-gcc配置了环境变量,为什么还是提 ...

  4. 利用matlab摄像机标定

    (1)输入图像 "Image names"键 Matlab的图形窗口显示出20幅靶标图像 (2) 提取角点 "Extract grid corners"键. 输 ...

  5. EXTJS4自学手册——EXT基本方法、属性(mixins多继承、statics、require)

    1.mixins 说明:类似于面向对象中的多继承 <script type="text/javascript"> Ext.onReady(function () {// ...

  6. MySQL-Front 建表引发的一点小思考(数据表格模版)

    我们建表的时候,有一些字段总是会常用到的.也就是每一张表都会有这些字段. 我用mysql有一点时间了,今天(2016-02-27 21:53:38)在用mysql-front建表的时候,感觉有点点不太 ...

  7. Matlab(2) -- Find()函数

    转自:http://www.matlabsky.com/thread-4228-1-1.html find函数:用于查询非零元素的行和列标志 语法: ind = find(X) ind = find( ...

  8. C1000k 新思路:用户态 TCP/IP 协议栈

    现在的服务器支撑上百万个并发 TCP 连接已经不是新闻(余锋2010年的演讲,ideawu 的 iComet 开源项目,WhatsApp 做到了 2.5M).实现 C1000k 的常规做法是调整内核参 ...

  9. s1=s1+1与s1+=1的区别

    刚看到一面试题,题目是这样的:short s1=1;s1=s1+1;有什么错?short s1=1;s1+=1;有什么错? 初看之下就是s1=s1+1和s1+=1的区别.在开发中我们基本上是使用后一种 ...

  10. ASP.NET c# textbox 正则表达式 文本框只允许输入数字(验证控件RegularExpressionValidator )

    <input type="text" name="test" onKeyUp="test1.value=(this.value=this.val ...