2011-09-20 84 views
4

我有一个列表框,每行都有删除按钮,所以当我单击删除按钮时,我需要点击列表框的索引以便删除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> 
+2

listboxName.SelectedIndex – MGZero

+0

但我想选择索引点击不在列表上的删除按钮 – Sujiz

回答

2

“当我点击删除按钮,我需要点击的指数”,因为每行有一个删除按钮,您应该指定索引的每删除“标签”属性按钮,所以无论何时单击删除按钮,您都将获得列表框的相应项目的索引。

对不起,我刚看到你的wp标签和你的xaml代码,所以我的回答可能是错误的。

+0

标签的想法很好 – gbianchi

+0

@unruledboy我如何使用标签专业版perty得到索引? – Sujiz

+0

@Sujiz您将按钮的标记设置为等于ListBox中单元格的索引 –

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

一个更好的选择是让列表框的数据绑定到列表或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}" 

然后,只需引用这些值作为描述

希望这有助于

相关问题