使用C#从Windows Phone的列表框中删除所选项目 [英] Delete selected item from listbox in windowsphone using c#

查看:108
本文介绍了使用C#从Windows Phone的列表框中删除所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用上下文菜单下拉框从列表框中删除所选项目,这是我的xaml

i want delete selected item from my listbox using context menu drop-down box here is my 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>

这是我要对删除事件"MenuItem_Click"执行的操作,但会引发错误只读集合不支持该操作.因此,删除该单击事件的所选项目的代码是什么

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

 // 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是页面上显示的对象列表.因此,此lstNews.Items是您的数据模板的集合,因此这就是为什么当您尝试lstNews.Items.Remove(lstNews.SelectedItem.ToString())时失败了.

lstNews.Items is list of object that are displayed on the page. 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.Remove(lstNews.SelectedItem)删除项目.

you should use lstNews.Items.Remove(lstNews.SelectedItem) to delete item.

但是为了最佳实践,最好从源中而不是从列表中删除项目.即您应该从fulllist中删除项目,然后将其重新分配为lstNews.ItemsSource = fulllist;

But for best practice it is prefered to delete item from the source not from the list. i.e. You should delete item from fulllist and reassign it as lstNews.ItemsSource = fulllist;

您的代码中所做的更改

  1. fulllist应该是

  1. fulllist should be a type of ObservableCollection so that all the changes done on data can be reflected to UI. To convert List to ObservableCollection Following code can be used:

fulllist = new ObservableCollection<NewsData>(new nList());

  • 添加用于从fulllist删除数据的实现,可能的实现可能是:

  • Add the implementation for deleting data from fulllist a possible implementation could be:

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

  • 这篇关于使用C#从Windows Phone的列表框中删除所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆