当前位置: 动力学知识库 > 问答 > 编程问答 >

wpf - "Operation not supported read-only collection" exception while adding controls dynamically to listbox items collection in windows phone

问题描述:

I am trying to add the button controls to listbox item collection dynamically while populating the listbox. I am getting the error saying "Operation not supported read-only collection".

the following is my xaml code and later is my xaml.cs code.

 <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);

});

}

Could someone please let me know the way to resolve the issue. Thanks in advance.

网友答案:
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); 

                }); 

        }

Convert this foreach to for loop. As you cannot modify the collection in foreach...

分享给朋友:
您可能感兴趣的文章:
随机阅读: