2011-09-20 81 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}" 

然後,只需引用這些值作爲描述

希望這有助於

相關問題