今天在做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>
所以,深入了解,才能更好地使用,继续学习。
时间: 2024-11-13 06:52:06