你有一个通过绑定绑定到列表框的列表,我试图从列表中删除/删除一个选定的项目,我收到此错误“只读集合不支持操作”
我的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>
{
}
}
选择已更改我尝试删除项目的位置
private void MyListBox__SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MyAccountX myaccntx = (sender as ListBox).SelectedItem as MyAccountX;
MyListBox_.Items.Remove(myaccntx);
}
答案 0 :(得分:0)
如果更改MyListBox的内容,则不会刷新视图。 需要注意的是,您必须更改List或Observable集合,并将其绑定到ListBox,并确保实现INotifyPropertyChanged接口。