Question

I am developing my first app on windows phone,I have an LongListSelector which cotains 3 elements, 2 elements are images and a textblock.Now I am able to populate the LongListSelector items,but i need to delete some items whenever its not required in the List

Items are removed based on whether we click on the image. My code is as shown.

  <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>

I need to remove the entire element from the List whenever the user clicks on Image with NAME=Button, Kindly Help me out in doing this as I am Pretty new to this field.

Was it helpful?

Solution

Add SelectionChanged event and do like below to get selected item.

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

}

EDITED:

private void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
   var element = (FrameworkElement)sender;
   YourListItemObject data = (YourListItemObject)element.DataContext;
  YourMainList.Remove(data);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top