none
listBox,Items collection must be empty before using ItemsSource RRS feed

  • 问题

  • Exception:
    invaildOperationException was unHandled :Items collection must be empty before using ItemsSource.
    Can anyone help me ?
    This is my code:
    xaml:
     <ListBox Height="478" HorizontalAlignment="Left" Margin="0,57,0,0" Name="listBox1" VerticalAlignment="Top" Width="450" SelectionChanged="listBox_SelectionChanged">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Text}" FontSize="25" VerticalAlignment="Center"/>
                        <Image Source="{Binding Source}" Width="250" Height="250" VerticalAlignment="Center"/>
                        <TextBlock Text="{Binding Text5}" FontSize="25" FontWeight="Bold"/>
                        <TextBlock Text="{Binding Text1}" FontSize="25" Width="450" TextWrapping="Wrap"/>
                        <StackPanel Orientation="Horizontal">
                            <Button Margin="20,0,0,0" Content="{Binding TextB1}" FontSize="25" Height="72" Name="btnComment" Width="200" Click="btnComment_Click" />
                            <Button Content="{Binding TextB2}" FontSize="25" Height="72" Name="btnCollect" Width="200" Click="btnCollect_Click" />
                        </StackPanel>
                    </StackPanel>
                </ListBox>


    C#:
    private void init()
            {
                List<Item> list = new List<Item>();
                Item item = new Item();
                item.Text = "XXXXXX"; 
                item.Source = new BitmapImage(new Uri("XX.jpg", UriKind.Relative));
                
                item.Text5 = "XXX";
                item.Text1 = "XXXX";

                item.TextB1 = "XX";
                item.TextB2 = "XX";
                list.Add(item);

                listBox1.ItemsSource = list;
    }

    tt
    2011年9月15日 10:28

全部回复

  • 我看您在Listbox中做了数据绑定,所以建议您对Listbox的item采用数据绑定。

    Listbox的ItemsSource绑定数据Items:<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">

    TextBlock 绑定Items中的LineOne数据项:<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>

    请参考如下的代码

    <controls:PanoramaItem Header="second item">
                    <!--Double line list with image placeholder and text wrapping-->
                    <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                    <!--Replace rectangle with image-->
                                    <Rectangle Height="100" Width="100" Fill="#FFE5001b" Margin="12,0,9,0"/>
                                    <StackPanel Width="311">                                   
                                        <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                        <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </controls:PanoramaItem>


    Xuesong Weibo http://weibo.com/highcedar

    2012年5月10日 5:20
    版主
  • 我看您在Listbox中做了数据绑定,所以建议您对Listbox的item采用数据绑定。

    Listbox的ItemsSource绑定数据Items:<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">

    TextBlock 绑定Items中的LineOne数据项:<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>

    请参考如下的代码

    <controls:PanoramaItem Header="second item">
                    <!--Double line list with image placeholder and text wrapping-->
                    <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                    <!--Replace rectangle with image-->
                                    <Rectangle Height="100" Width="100" Fill="#FFE5001b" Margin="12,0,9,0"/>
                                    <StackPanel Width="311">                                   
                                        <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                        <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </controls:PanoramaItem>


    Xuesong Weibo http://weibo.com/highcedar


    如何获得这个ListBox中StackPanel中TextBlock中的数据。。。
    备注:这个是数据绑定的,怎么用C#语言获取数据
    • 已编辑 mr.shuai 2012年11月15日 12:55
    2012年11月15日 12:54