简体   繁体   English

从Lisbox WP8 C#中删除所选项目

[英]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 我的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. 如果更改MyListBox的内容,则不会刷新视图。 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. 因此,必须更改要绑定到ListBox的List或Observable集合,并确保要实现INotifyPropertyChanged接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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