Sujiz
Sujiz

Reputation: 1740

How to get the index of the clicked button in a listbox

I have a list box having delete button in each row so when i clicked the delete button i need the clicked index of list box so as to delete the row.how can i get the index of the clicked item?

here is my listbox

 <ListBox  HorizontalAlignment="Left" Name="listBox1" Margin="-3,132,0,0" VerticalAlignment="Top" Width="498" SelectionChanged="listBox1_SelectionChanged">

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderThickness="0,1,0,0" BorderBrush="#FFC1BCBC" Width="490">
                        <Grid Height="70">
                            <Image Height="50" 
                           HorizontalAlignment="Left" 
                           Name="image" 
                           Stretch="Fill" 
                           VerticalAlignment="Top" 
                           Width="50" 
                           Source="{Binding iconPath}" Margin="8,8,0,0" />
                            <TextBlock Name="Name" Text="{Binding displayName}" VerticalAlignment="Center" Margin="60,0,0,0" Foreground="Black" FontWeight="SemiBold"></TextBlock>

                            <Button Name="btnDeleteRow" Width="50" Click="btnDeleteDashboard_Click" Margin="390,0,0,0" BorderBrush="Transparent" Style="{StaticResource logoutbtn_style}">

                            </Button>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Upvotes: 4

Views: 5272

Answers (3)

Darkside
Darkside

Reputation: 1739

A better option is to have the list box databound to a List or ObservableObject collection, then also two way databind the "SelectedItem" or "SelectedIndex" (I prefer selecteditem) to a property.

Then on clicking of the Button you can simply call collection.Remove(selecteditemproperty).

If you are using MVVM or iPropertyNotified then the view will automatically update the list when you change the backend collection.

Let me know if you need a more detailed example for this. but basically:

    public ObservableCollection<ItemViewModel> _items;
    /// <summary>
    /// A collection for ItemViewModel objects.
    /// </summary>
    public ObservableCollection<ItemViewModel> Items
    {
        get
        {
            return _items;
        }
        set
        {
            if (value != _items)
            {
                _items = value;
                NotifyPropertyChanged("Items");
            }
        }
    }

    private ItemViewModel _listBoxSelectedItem;
    /// <summary>
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding
    /// </summary>
    /// <returns></returns>
    public ItemViewModel ListBoxSelectedItem
    {
        get
        {
            return _listBoxSelectedItem;
        }
        set
        {
            if (value != _listBoxSelectedItem)
            {
                _listBoxSelectedItem = value;
                NotifyPropertyChanged("ListBoxSelectedItem");
            }
        }
    }

Then Bind the listbox like this:

 ItemsSource="{Binding Items}" SelectedItem="{Binding ListBoxSelectedItem, Mode=TwoWay}" 

Then just reference these values as described

Hope this helps

Upvotes: 3

unruledboy
unruledboy

Reputation: 2342

" when i clicked the delete button i need the clicked index", since each row has a delete button, you should assign index to the "Tag" property of each delete button, so whenever you click a delete button, you get the index of correpsponding item of the listbox.

sorry, I just saw your wp tag and your xaml code, so my answer could be wrong.

Upvotes: 2

ColinE
ColinE

Reputation: 70170

I assume your ListBox is databound to some source collection? If this is the case, the DataContext of your button will be an instance of one of your bound items. You can then do as follows:

// if for example you bind a list of MyDataObject instances ...

// create a list
List<MyDataObject> myDataObjects = CreateTestData();

// bind it
listBox1.ItemSource = myDataObjects;

...

// in your click handler
private void btnDeleteDashboard_Click(object sender, EventArgs args)
{
  // cast the sender to a button
  Button button = sender as Button;

  // find the item that is the datacontext for this button
  MyDataObject dataObject = button.DataContent as MyDataObject;

  // get the index
  int index = myDataObjects.IndexOf(dataObject);
}

Upvotes: 5

Related Questions