简体   繁体   English

使用C#从Windows Phone的列表框中删除所选项目

[英]Delete selected item from listbox in windowsphone using c#

i want delete selected item from my listbox using context menu drop-down box here is my xaml 我想使用上下文菜单下拉框从列表框中删除所选项目,这是我的XAML

  <ListBox Margin="3,60,1,10" Grid.Row="1" Name="lstNews" Tap="lstNews_Tap" Width="476" d:LayoutOverrides="VerticalMargin">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Grid.Row="1" Orientation="Horizontal" Height="120" Width="478">
                    <StackPanel.Background>
                        <ImageBrush ImageSource="Images/Text-ALU.png" Stretch="Fill" />
                    </StackPanel.Background>


                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu>
                            <toolkit:MenuItem Header="Delete" Click="MenuItem_Click"/>
                            <toolkit:MenuItem Header="Open" Click="MenuItem_Click"/>

                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>


                    <Grid HorizontalAlignment="Left" Width="30" Background="#FF0195D5" Margin="0,0,0,2" Height="118">
                        <TextBlock x:Name="txtDate" TextWrapping="Wrap" Text="{Binding Path=newsDate}" RenderTransformOrigin="0.5,0.5" Margin="-43.169,44.001,-43.831,0" UseLayoutRounding="False" d:LayoutRounding="Auto" TextAlignment="Center" Height="30" VerticalAlignment="Top" Width="117">
                            <TextBlock.RenderTransform>
                                <CompositeTransform Rotation="-90"/>
                            </TextBlock.RenderTransform>
                        </TextBlock>
                    </Grid>
                    <Grid HorizontalAlignment="Left" Width="5" Height="120"/>
                    <StackPanel Orientation="Vertical" VerticalAlignment="Top" Width="432" Height="114">
                        <TextBlock x:Name="txtTitle" Height="27" TextWrapping="Wrap" Text="{Binding Path=newsTitle}" Foreground="Black" FontSize="18.667" HorizontalAlignment="Left" Width="432" FontWeight="Bold" />
                        <StackPanel Orientation="Horizontal" Width="432" Height="27">
                            <TextBlock x:Name="txtBy" Height="27" TextWrapping="Wrap" Text="{Binding Path=newsSource}" Foreground="Black" FontSize="18.667" Width="399"/>
                            <Image x:Name="imgArrow" Width="25" Source="Images/Go-In-Arrow.png" Height="25" Margin="5,0,0,0"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Width="433" Height="60">
                            <TextBlock x:Name="txtDesc" Height="58" TextWrapping="Wrap" Foreground="Black" Text="{Binding Path=newsShortDescription}" FontSize="18.667" Width="371"/>
                            <TextBlock x:Name="txtID" Height="56" Text="{Binding Path=newsID}"  TextWrapping="Wrap" Foreground="Black" FontSize="18.667" Width="8" Visibility="Collapsed"/>
                            <Image x:Name="imgType" Width="35" Source="{Binding Path=newsTypeImage}" Height="40" Margin="27,20,0,0" d:LayoutOverrides="Height"/>
                        </StackPanel>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

here is what i am trying to do on delete event "MenuItem_Click" but than it throws error "Operation not supported on read-only collection.so what is code to delete selected item on that click event 这是我要对删除事件“ MenuItem_Click”执行的操作,但是它会引发错误“只读collection.supported不支持该操作。因此,删除该单击事件的所选项目的代码是什么

 // this is code to delete i am trying-->  lstNews.Items.Remove(lstNews.SelectedItem.ToString());
      //below is code  im trying to set listboxitemsource
          private void FillListBox()
    {
        fulllist = new nList();
        lstNews.ItemsSource = fulllist;

    }

 public class nList : List<NewsData>
{
    StringData sd = new StringData();
    public IList<NewsData> GetLCList()
    {
        IList<NewsData> lcList = null;
        using (NewsDataContext context = new NewsDataContext(sd.news_string))
        {
            IQueryable<NewsData> query = (from app in context.NewsData select app).OrderByDescending(app => app.entryID);
            lcList = query.ToList();
        }
        return lcList;
    }

    public nList()
    {
        IList<NewsData> lcData = this.GetLCList();
        StringBuilder messageBuilder = new StringBuilder();
        foreach (NewsData lc in lcData)
        {

            Add(new NewsData
            {
                newsID = lc.newsID,
                newsTitle = lc.newsTitle,
                newsSource = lc.newsSource,
                newsDate = (new GetDate()).getdate(lc.newsDate),//(new AnnouncementList()).getdate(lc.newsDate),
                newsShortDescription = lc.newsShortDescription,
                newsTypeImage = lc.newsTypeImage,
                newsSharing = lc.newsSharing
            });
        }
    }
}

lstNews.Items is list of object that are displayed on the page. lstNews.Items是页面上显示的对象列表。 So this lstNews.Items is collection of your datatemplate so that is why when you tried lstNews.Items.Remove(lstNews.SelectedItem.ToString()) than this fails. 因此,此lstNews.Items是数据模板的集合,因此这就是为什么当您尝试lstNews.Items.Remove(lstNews.SelectedItem.ToString())失败了。

you should use lstNews.Items.Remove(lstNews.SelectedItem) to delete item. 您应该使用lstNews.Items.Remove(lstNews.SelectedItem)删除项目。

But for best practice it is prefered to delete item from the source not from the list. 但从最佳实践来看,最好从源而不是从列表中删除项目。 ie You should delete item from fulllist and reassign it as lstNews.ItemsSource = fulllist; 即,您应该从fulllist删除项目,然后将其重新分配为lstNews.ItemsSource = fulllist;

Changes done in your code 代码中所做的更改

  1. fulllist should be a type of ObservableCollection so that all the changes done on data can be reflected to UI. fulllist应该是ObservableCollection的一种,以便对数据所做的所有更改都可以反映到UI。 To convert List to ObservableCollection Following code can be used: 将List转换为ObservableCollection可以使用以下代码:

     fulllist = new ObservableCollection<NewsData>(new nList()); 
  2. Add the implementation for deleting data from fulllist a possible implementation could be: 添加用于从fulllist删除数据的实现,可能的实现可能是:

     object obj = lstNews.SelectedItem; if(obj is NewsData){ fulllist.Remove((NewsData)obj); lstNews.ItemsSource = fulllist; } 

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

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