删除Longlistselector中的选定元素?

时间:2014-05-14 06:33:39

标签: c# xaml windows-phone-8

我在Windows手机上开发我的第一个应用程序,我有一个LongListSelector,它包含3个元素,2个元素是图像和textblock。现在我可以填充LongListSelector项目,但我需要删除一些项目,不管它不是列表中要求

根据我们是否点击图片删除项目。我的代码如图所示。

  <phone:LongListSelector x:Name="MainLongListSelector" DataContext="{Binding listData}" IsGroupingEnabled="False" >
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Grid.Column="0">
                        <Image Name="condition" Source="{Binding Imagetype}" Height="100"  />
                        <StackPanel Grid.Column="1">
                            <TextBlock Text="{Binding Country}" TextWrapping='Wrap' Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="30"/>
                            <TextBlock Text="{Binding Temp}" TextWrapping="Wrap"
                                       Style="{StaticResource PhoneTextExtraLargeStyle}"
                                       FontSize="30"
                                       ></TextBlock>
                        </StackPanel>
                        <StackPanel HorizontalAlignment="Right" Grid.Column="2">
                            <Image Name="Button" Source="{Binding Remove}" Height="100" Stretch="UniformToFill" HorizontalAlignment="Right"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>

每当用户点击带有NAME = Button的图片时,我都需要从列表中删除整个元素,请帮助我这样做,因为我对这个领域很新。

1 个答案:

答案 0 :(得分:0)

添加SelectionChanged事件,并执行以下操作以获取所选项目。

private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     if (e.AddedItems.Count > 0)
     {
        YourListItemObject item = e.AddedItems[0] as YourListItemObject;
        YourMainList.Remove(item);
     }

}

<强>编辑:

private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
   var element = (FrameworkElement)sender;
   YourListItemObject data = (YourListItemObject)element.DataContext;
  YourMainList.Remove(data);
}