2

I have a listbox,the data in listbox is getting populated through sqlite database.

XAML

enter image description here

<ListBox x:Name="listBoxobj" Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" SelectionChanged="listBoxobj_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Width="330" Height="100" >
                <Border Margin="5" BorderBrush="White" BorderThickness="1">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                        <CheckBox Name="Option1CheckBox" Grid.Row="0" Grid.Column="0" IsChecked="{Binding _isComplete}"  VerticalAlignment="Center" Margin="5,0,0,0" />
                        <TextBlock x:Name="NameTxt" Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" FontSize="22" Foreground="White"/>
                        <TextBlock x:Name="Age" Grid.Row="1" Grid.Column="1" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" FontSize="22" Text="{Binding Age}" />
                        <Button Grid.Row="0" Grid.Column="2" x:Name="deleteTaskButton" BorderThickness="0" Margin="0"  Click="deleteTaskButton_Click" Height="18">
                            <Image Source="/Assets/delete.png"/>
                        </Button>
                    </Grid>
                </Border>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

When the user clicks on deleteTaskButton which is present in each listitem,the listboxitem data should get deleted from the list and move to another list present in next Pivot page.How can I do that? When I click on the delete button the list is not getting selected but the delete icon is selected.

1
  • What does your deleteTaskButton_Click() method look like? Commented Aug 2, 2017 at 4:47

1 Answer 1

0

So as there was no information on the data types of the data contained in the listview. I've written down how your deleteTaskButton_Click method should look. Replace YourDataType with your data type and secondListBoxobj with the name of the second listbox

 private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
    {
        var dataContext = (sender as Button).DataContext as YourDataType;
        if (dataContext != null)
        {
            //now you have the item that was clicked to delete. 
            var DeleteFromDelete = listBoxobj.ItemsSource as ICollection<YourDataType>;
            if (DeleteFromDelete != null)
            {
                //this removes the item to be removed from the currently viewing listview.
                DeleteFromDelete.Remove(dataContext);

                //now add the item that was deleted into the other listview.
                var TobeAddedIntoList = secondListBoxobj.ItemsSource as ICollection<YourDataType>;
                if (TobeAddedIntoList != null)
                    TobeAddedIntoList.Add(dataContext);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.