MVVM 在使用 ItemsSource 之前,项集合必须为空
今天在做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 之前,项集合必须为空的更多相关文章
- WPF 在使用 ItemsSource 之前,项集合必须为空
原文:WPF 在使用 ItemsSource 之前,项集合必须为空 <DataGrid x:Name="datagrid" ItemsSource="{Bindin ...
- 【WPF异常】在使用 ItemsSource 之前,项集合必须为空
<DataGrid x:Name=" AutoGenerateColumns="False" GridLinesVisibility="None" ...
- 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控件就遇到了比 ...
- java 对象、集合的非空判断
自我总结,有什么不到位的地方,请各位纠正补充,感激不尽! 目的:使程序更严谨 ***对象验证是否不为空: if( null != obj ) ***List验证不为空:if( null != lis ...
- 判断集合不为空Java
list.size>0判断集合size是否大于0 list.isEmpty()判断集合是否为空,返回布尔值
- 判断list集合不为空
在java开发中新手容易将判断一个list集合是否为空,只以If(list!=null)去判断,且容易和isEmpty()混淆,但是,list集合为空还是为null,是有区别的. 先看一下下面的例子, ...
- 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空
通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...
随机推荐
- 关于 The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine. 异常处理
导入Excel在本地环境没有问题,但部署到服务器上后出现异常. 经排查,是系统问题,因为本地是32位系统,而服务器上则是64位系统. 解决方法: 1.打开IIS管理器 2.右击应用程序所在的连接池 3 ...
- 一天一经典Reducing the Dimensionality of Data with Neural Networks [Science2006]
别看本文没有几页纸,本着把经典的文多读几遍的想法,把它彩印出来看,没想到效果很好,比在屏幕上看着舒服.若用蓝色的笔圈出重点,这篇文章中几乎要全蓝.字字珠玑. Reducing the Dimensio ...
- 接入百度语音SDK的步骤
1.导入依赖库 SystemConfiguration.framework AudioToolbox.framework UIkit.framework AVFoundation.framework ...
- Java(类与对象)
1>对象判等 请输入并运行以下代码,得到什么结果? public class Test { public static void main(String[] args) { // TODO Au ...
- Nginx-->基础-->排错-->nginx错误总结
一.启动时错误 1.错误提示: 2016/11/16 17:36:41 [emerg] 2458#2458: getpwnam("nginx") failed 查看错误日志文件内容 ...
- PHP操作Mongodb之高级查询篇
本文主要讲解PHP中Mongodb的除了增删改查的一些其他操作. 在PHP操作Mongodb之增删改查篇中我们介绍了PHP中Mongodb的增加.删除.修改及查询数据的操作.本文主要是将查询时用到的高 ...
- 您还有心跳吗?超时机制分析(java)
注:本人是原作者,首发于并发编程网(您还有心跳吗?超时机制分析),此文结合那里的留言作了一些修改. 问题描述 在C/S模式中,有时我们会长时间保持一个连接,以避免频繁地建立连接,但同时,一般会有一个超 ...
- ue4 shooterGame 第一步 搭建git linux服务器
1.分别在linux(服务器)上安装git.和openssh服务, 在windows(客户机)上安装cygwin,模拟linux环境以及安装windows git客户端. 2.windows的cygw ...
- 异步I/O操作
今天在看boost库的时候注意到异步I/O操作时,缓冲区有效性问题. 如何实现异步操作:以异步读操作为例async_read(buffer, handler): void handler() {} v ...
- ASP.NET Web API 2 入门
本文参考:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web ...