MVVM 在使用 ItemsSource 以前,項集合必須爲空

今天在作ListBox和Combobox綁定的時候,都出現過「在使用 ItemsSource 以前,項集合必須爲空」的錯誤。html

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事件的時候代碼有誤,修改爲下面的就能夠了:spa

  <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是想要橫向展現圖片而且自動換行,代碼以下:code

 <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(),與數據源無關。htm

查了不少資料,其中有一篇:http://www.verydemo.com/demo_c441_i91243.htmlblog

看了一下,以爲可能與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>

因此,深刻了解,才能更好地使用,繼續學習。get

相關文章
相關標籤/搜索