简体   繁体   中英

Remove Selected Item from Lisbox WP8 C#

Hi have a list bound to a Listbox through binding, i am trying to remove/delete a selected item from the list and i get this error "Operation not supported on read-only collection"

My Xaml

                  bam:ListAnimation.LoadItem="{StaticResource ListBoxAnimation1}" 
                  bam:ListAnimation.LoadItemDelay="0.1" SelectionChanged="MyListBox__SelectionChanged"  
                  >
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate >
                        <Grid Margin="0,4,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>

                            <Border HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="2"  Margin="0,0,0,10" Width="60" Height="60">
                                <Image   Source="/Assets/delete.png" Height="50" Width="50" Stretch="UniformToFill"/>
                            </Border>
                            <StackPanel Grid.Column="1" Margin="0,0,0,0">
                                <TextBlock Margin="10,10,0,0" Text="{Binding AccountName}" TextWrapping="Wrap" FontSize="18" FontWeight="Normal" Width="200" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Segoe UI" Foreground="Black"/>
                                <TextBlock Margin="10,0,0,0" Text="{Binding  AccountNumber}" TextWrapping="Wrap" FontSize="15" FontWeight="Thin" Width="200" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Segoe UI" Foreground="Black"/>
                                <!--<Border BorderBrush="SkyBlue" Margin="0,17,0,0" BorderThickness="0,0,0,2" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" />-->
                                <!--<TextBlock Text="{Binding MerchantId}"  Foreground="Blue" TextWrapping="NoWrap"/>-->
                            </StackPanel>
                            <Border Grid.ColumnSpan="2" BorderBrush="Gray" Margin="0,0,0,0" BorderThickness="0,0,0,1" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" />
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

My Class

public  class MyAccountX
{
    public string Name { get; set; }
    public string AccountName { get; set; }
    public string AccountNumber { get; set; }
    public string Merchantid { get; set; }
    public class UserList : ObservableCollection<MyAccountX> 
    {

    }
}

Selection Changed Where i try to delete the item

private void MyListBox__SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MyAccountX myaccntx = (sender as ListBox).SelectedItem as MyAccountX;

        MyListBox_.Items.Remove(myaccntx);
    }

If you change the content of the MyListBox it will not refresh the view. Insted of that you have to change the List or Observable collection you are Binding to the ListBox and be sure you are implementing the INotifyPropertyChanged Interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM