从Lisbox WP8 c#中移除选中的项目

本文关键字:项目 WP8 Lisbox | 更新日期: 2023-09-27 18:04:13

我有一个列表通过绑定绑定到一个列表框,我试图从列表中删除/删除一个选定的项目,我得到这个错误"只读集合不支持操作"

我的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>
我班上

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> 
    {
    }
}

select Changed Where i try delete item

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

从Lisbox WP8 c#中移除选中的项目

如果你改变了MyListBox的内容,它将不会刷新视图。相反,你必须改变列表或Observable集合你绑定到ListBox,并确保你正在实现INotifyPropertyChanged接口。