user1237131
user1237131

Reputation: 1863

Listbox delete item functionality?

How to find the item id when deletion of item in listbox?

<DataTemplate x:Key="ToDoListBoxItemTemplate">

            <Grid HorizontalAlignment="Stretch" Width="420">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  Width="30"/>
                    <ColumnDefinition Width="*" />
                   <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="100" />
                </Grid.ColumnDefinitions>

                <!--<CheckBox 
                    IsChecked="{Binding IsComplete, Mode=TwoWay}" 
                    Grid.Column="0" VerticalAlignment="Top"/>-->

                <TextBlock 
                    Text="{Binding subcategname}" 
                    FontSize="25" 
                    Grid.Column="1" Grid.ColumnSpan="2" 
                    VerticalAlignment="Top" Margin="-36, 12, 0, 0"/>

                <Button                                
                    Grid.Column="3"
                    x:Name="deleteTaskButton"
                    BorderThickness="0"                                                                  
                    Margin="0, -18, 0, 0" Click="deleteTaskButton_Click">

                    <Image 
                    Source="Images/appbar.delete.rest.png"
                    Height="75"
                    Width="75"/>

                </Button>
            </Grid>
        </DataTemplate>




<ListBox x:Name="FinanceListBox" Margin="0,0,-12,0" ItemsSource="{Binding}"  ItemContainerStyle="{StaticResource ListBoxItemStyle1}" ItemTemplate="{StaticResource ToDoListBoxItemTemplate}"  SelectionChanged="FinanceList_SelectionChanged">
                </ListBox>

in SelectionChanged event i wrote the following code.

private void FinanceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            System.Collections.IList list = e.AddedItems;
            if (e.AddedItems.Count == 1)
            {
                IEnumerable<Category> categs = list.Cast<Category>();
                Category em = categs.ElementAt<Category>(0);
                int id = em.id;
                int categoryid = em.categoryid;
                string subcategoryname = em.subcategname;

                NavigationService.Navigate(new Uri(String.Format("/SubCategories.xaml?id=" + id + "&categoryid=" + categoryid + "&subcategoryname=" + subcategoryname), UriKind.Relative));
            }
        }




 private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
        {


        }

But how to find the particular item id when deleting the item. How to find the particular id to delete the item?

Upvotes: 0

Views: 415

Answers (3)

Shawn Kendrot
Shawn Kendrot

Reputation: 12465

No need to assign a tag to you Button and make your xaml ugly, you can get the DataContext from the button.

private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
{
     Button deleteButton = (Button)sender; 
     var entity = deleteButton.DataContext as MyEntity;

     //Delete entity
}

Upvotes: 0

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

Apply tag to the button.

<Button                                
                    Grid.Column="3"
                    x:Name="deleteTaskButton"
                    BorderThickness="0"  Tag="{Binding id}"                                                                
                    Margin="0, -18, 0, 0" Click="deleteTaskButton_Click">

In code: as Euqene has mentioned:

private void deleteTaskButton_Click(object sender, RoutedEventArgs e)
        {
         Button b = sender as Button; 
         var id = (int)b.Tag;    
         //now do what you want with id
        }

Upvotes: 2

Eugene
Eugene

Reputation: 2985

One of the ways is to bind the Tag property of the button to a meaningful identifier, then in your delete method use the following code:

Button b = sender as Button; //and now delete from your collection where id = b.Tag

Upvotes: 2

Related Questions