如何在列表框中获取所点击按钮的索引值

4

我有一个列表框,每一行都有一个删除按钮。当我点击删除按钮时,我需要获取列表框中被点击的行的索引以便删除这一行。怎么获取被点击项的索引?

以下是我的列表框:

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

但是我希望在单击删除按钮时选择索引,而不是在列表上选择。 - Sujiz
3个回答

5

我假设你的ListBox是绑定到某个源集合的?如果是这种情况,按钮的DataContext将成为一个绑定项实例。然后可以按照以下方式进行操作:

// 例如,如果您绑定了MyDataObject实例列表...

// 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);
}

3
更好的选择是将列表框数据绑定到一个List或ObservableObject集合上,然后使用“SelectedItem”或“SelectedIndex”(我更喜欢使用selecteditem)进行双向数据绑定到属性。
然后,在点击按钮时,您可以简单地调用collection.Remove(selecteditemproperty)。
如果您正在使用MVVM或iPropertyNotified,则在更改后端集合时,视图将自动更新列表。
如果需要更详细的示例,请告诉我。但基本上就是这样。
    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");
            }
        }
    }

然后像这样绑定列表框:
 ItemsSource="{Binding Items}" SelectedItem="{Binding ListBoxSelectedItem, Mode=TwoWay}" 

接下来只需按照描述引用这些值即可。

希望这能帮到你。


2

“当我点击删除按钮时,我需要被点击的索引”,因为每一行都有一个删除按钮,你应该在每个删除按钮的“Tag”属性中分配索引,这样每当你点击一个删除按钮时,你就可以获取相应列表框项的索引。

抱歉,我刚看到你的wp标签和xaml代码,所以我的答案可能是错误的。


@unruledboy 如何使用 tag 属性获取索引? - Sujiz
@Sujiz,你将按钮的标签设置为 ListBox 中单元格的索引。 - William Melani
因为按钮是列表项的父级,所以我们无法从按钮访问 ListItem 吗? - MBen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接