如何获取列表框中单击按钮的索引

时间:2011-09-20 13:07:12

标签: c# windows-phone-7 listbox

我有一个列表框,每行都有删除按钮,所以当我点击删除按钮时,我需要点击列表框的索引,以便删除row.how我可以获得点击项目的索引吗?

这是我的列表框

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

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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 :(得分:2)

“当我点击删除按钮时我需要点击的索引”,因为每行都有一个删除按钮,你应该为每个删除按钮的“标签”属性分配索引,所以每当你点击一个删除按钮,你就会得到列表框的相应项目索引。

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