向Windows Phone中的列表框项集合动态添加控件时,“操作不支持只读集合”异常

时间:2012-01-05 07:33:25

标签: wpf silverlight windows-phone-7.1

我正在尝试在填充列表框时动态地将按钮控件添加到列表框项目集合中。我收到错误说“操作不支持只读集合”。 以下是我的xaml代码,后来是我的xaml.cs代码。

        <ListBox x:Name="TripList" Height="465" HorizontalAlignment="Left" VerticalAlignment="Top" Width="456" Background="White" Foreground="Blue">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border BorderBrush="Black" BorderThickness="0,0,0,4">
                                <StackPanel Orientation="Vertical" Width="456">
                                    <StackPanel Orientation="Vertical">
                                        <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                            <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" Width="370" FontSize="24" Text="{Binding PUDetails}"/>
                                            <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="24" Text="{Binding TripStatus}"/>
                                        </StackPanel>
                                        <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal" Width="300">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Conf: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding ConfNumber}"/>
                                            </StackPanel>
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Est Do Tm: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding DOTime}"/>
                                            </StackPanel>
                                        </StackPanel>
                                        <StackPanel VerticalAlignment="Top" Orientation="Vertical">
                                            <StackPanel x:Name="stacktest" Background="Azure" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Svc: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding TripService}"/>
                                            </StackPanel>
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="PU: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding PURoute}"/>
                                            </StackPanel>
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="DO: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding DORoute}"/>
                                            </StackPanel>
                                            <StackPanel VerticalAlignment="Top" Orientation="Horizontal">
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Pax: "/>
                                                <TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding PaxDetails}"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </StackPanel>

                                        </StackPanel>
                                 </StackPanel>
                            </Border>

                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>


       Grid grdtest = new Grid();
        foreach (TripsList lst in TripList.Items)
        {

            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {

                    grdtest.RowDefinitions.Add(new RowDefinition() { });
                    grdtest.ColumnDefinitions.Add(new ColumnDefinition() { });
                    Button button = new Button();
                    button.HorizontalAlignment = HorizontalAlignment.Right;
                    button.VerticalAlignment = VerticalAlignment.Top;
                    button.Margin = new Thickness(3);
                    button.Width = 80;
                    button.Height = 25;
                    button.Content = "btn1";
                    Grid.SetRow(button, 0);
                    Grid.SetColumn(button, 0);
                    grdtest.Children.Add(button);

                    LayoutRoot.Children.Add(grdtest);
                    TripList.Items.Add(grdtest);

                });

        }

有人可以告诉我解决问题的方法。提前谢谢。

1 个答案:

答案 0 :(得分:0)

foreach (TripsList lst in TripList.Items) 
        { 

            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => 
                { 

                    grdtest.RowDefinitions.Add(new RowDefinition() { }); 
                    grdtest.ColumnDefinitions.Add(new ColumnDefinition() { }); 
                    Button button = new Button(); 
                    button.HorizontalAlignment = HorizontalAlignment.Right; 
                    button.VerticalAlignment = VerticalAlignment.Top; 
                    button.Margin = new Thickness(3); 
                    button.Width = 80; 
                    button.Height = 25; 
                    button.Content = "btn1"; 
                    Grid.SetRow(button, 0); 
                    Grid.SetColumn(button, 0); 
                    grdtest.Children.Add(button); 

                    LayoutRoot.Children.Add(grdtest); 
                    TripList.Items.Add(grdtest); 

                }); 

        }

将此 foreach 转换为 for 循环。因为你不能在foreach中修改集合......