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 ...
随机推荐
- ruby Matrix 输出 格式化
require 'matrix' class Matrix def to_pretty_s s = "" i = 0 while i < self.column_size s ...
- JVM内存模型和启动参数的关系
今天开始接触JVM的内存模型这一块的内容,以下这张图是从网上找的,先收藏了,虽然现在还看不太懂.以后弄懂了才进行详细的解说.
- protobuf初体验
概念介绍 Protocol buffers 是google公司的与语言无关.与平台无关的.可扩张的为序列化话结构数据,就像xml一样,办事更加的小巧.快速.简单.Protocol buffers 目前 ...
- 【openresty】获取post请求数据FormInputNginxModule模块
关于openresty的一些介绍看这里. 首先,实验背景为openresty作为后台来处理前台post传递的数据. 在openresty内,有一个FormInputNginxModule模块,作用是解 ...
- java Thumbnails 加载网络图片,处理返回base64
URL url = new URL("图片网络地址"); BufferedInputStream in = new BufferedInputStream(url.openStre ...
- C# 模拟上传图片
上传图片的格式一定要按规定的写,不然没办法正确上传的. 我在上传的时候就是值前面没有空一行,导致上传不成功,很纠结的错误. 我要模拟的是一个FLASH的上传控件,我开始用HttpAnalyze抓包是抓 ...
- 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
两种原因: 第一种为程序的运行以平台系统位数不匹配,第二种则是该死的VS整出来的... 一般在下面三种情景下会发生. 1.64位系统上C#调用32位的C++ *.dll 2.64位系统上IIS发布含有 ...
- NPOI导出Excel(含有超过65335的处理情况)
NPOI导出Excel的网上有很多,正好自己遇到就学习并总结了一下: 首先说明几点: 1.Excel2003及一下:后缀xls,单个sheet最大行数为65335 Excel2007 单个sheet ...
- asp.net下简单的Epplus导出excel
引用的命名空间 using System.IO; using OfficeOpenXml; /// <summary> /// 导出excel /// </summary> / ...
- SSMS错误:A connection was successfully established with the server, but then an error occurred during the login process
参考: 系统太慢,实在搞不清是哪里的问题,祭出重装大法 需要安装的工具还真多,先装主要的吧.VS2013, SQL SERVER 2012,搞定.. 连个数据库试试,出错了: A connection ...