zoukankan      html  css  js  c++  java
  • 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>

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

  • 相关阅读:
    数学专业的数学与计算机专业的数学的比较(转)
    vim的复制粘贴小结(转)
    (转)会议期刊论文发表介绍(计算机科学领域)
    字符串最后一个单词的长度(华为在线训练)
    (转)初识suse——linux
    (转)C++中返回对象的情形及RVO
    mystring c++ 自己的string 封装
    std::string
    FTP 1.0
    conflicting types for xxxx错误 (转)
  • 原文地址:https://www.cnblogs.com/xiamojinnian/p/4584154.html
Copyright © 2011-2022 走看看