2013-07-01 32 views
0

我有列表框,其中包含垂直列出的按鈕和相關數據,如下所示;找到列表項索引 - WP7

<ListBox Name="CTransactionList" Margin="0,0,0,0" > 
        <ListBox.ItemTemplate > 
         <DataTemplate> 
          <Button Width="400" Height="200" Background="#6A040B2E" Click="completetransact"> 
           <Button.Content > 
            <StackPanel Orientation="Horizontal" Height="200" Width="400"> 
             <Image Source="{Binding Type1}" Width="80" Height="80" VerticalAlignment="Top" Margin="0,40,0,0"/> 
             <StackPanel Orientation="Vertical" Height="200"> 
              <StackPanel Orientation="Horizontal" Height="30"> 
               <TextBlock Width="100" FontSize="22" Text="Name:" Height="30"/> 
               <TextBlock Width="200" FontSize="22" Text="{Binding Date1}" Height="30"/> 

              </StackPanel> 
              <StackPanel Orientation="Horizontal" Height="30"> 
               <TextBlock Width="100" FontSize="22" Text="Difficulty:" Height="30"/> 
               <TextBlock Width="200" FontSize="22" Text="{Binding Amount1}" Height="30"/> 
              </StackPanel> 
              <StackPanel Orientation="Horizontal" Height="30"> 
               <TextBlock Width="110" FontSize="22" Text="TotalTime:" Height="30"/> 
               <TextBlock Width="200" FontSize="22" Text="{Binding Time1}" Height="30"/> 
              </StackPanel> 
              <StackPanel Orientation="Horizontal" Height="30"> 
               <TextBlock Width="100" FontSize="22" Text="Distance:" Height="30"/> 
               <TextBlock Width="200" FontSize="22" Text="{Binding Dis1}" Height="30"/> 
              </StackPanel> 
              <StackPanel Orientation="Horizontal" Height="65"> 

               <TextBlock Width="290" FontSize="14" Text="{Binding Def1}" Height="65" TextWrapping="Wrap" FontStyle="Italic"/> 
              </StackPanel> 
             </StackPanel> 
            </StackPanel> 
           </Button.Content> 
          </Button> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 

       </ListBox> 

而我用這些類綁定數據;

[DataContract] 
    public class CTransaction 
    { 
     [DataMember] 
     public String Date1 { get; set; } 
     [DataMember] 
     public String Amount1 { get; set; } 
     [DataMember] 
     public String Type1 { get; set; } 
     [DataMember] 
     public String Time1 { get; set; } 
     [DataMember] 
     public String Dis1 { get; set; } 
     [DataMember] 
     public String Def1 { get; set; } 
     [DataMember] 
     public String Cdate1 { get; set; } 
     [DataMember] 
     public String Strpt1 { get; set; } 
     [DataMember] 
     public String Endpt1 { get; set; } 
     [DataMember] 
     public int Index1 { get; set; } 

     public CTransaction(String date1, String amount1, String type1, String time1, String dis1, String def1, String cdate1, String strpt1, String endpt1,int index1) 
     { 
      this.Date1 = date1; 
      this.Amount1 = amount1; 
      this.Time1 = time1; 
      this.Dis1 = dis1; 
      this.Def1 = def1; 
      this.Cdate1 = cdate1; 
      this.Strpt1 = strpt1; 
      this.Endpt1 = endpt1; 
      this.Index1 = index1; 
      switch (type1) 
      { 
       case "FR": 
        this.Type1 = "Images/a.png"; 
        break; 

       case "TA": 
        this.Type1 = "Images/b.png"; 
        break; 

       case "DA": 
        this.Type1 = "Images/c.png"; 
        break; 

       case "CC": 
        this.Type1 = "Images/mount.png"; 
        break; 
      } 
     } 
    } 

如上所示,我有一個名爲Index1的數據綁定,該數據綁定使用任意整數爲按鈕編制索引。我想用這段代碼刪除用戶指定的索引按鈕;我確切的願望是刪除點擊按鈕(即如果第二個按鈕被點擊,然後刪除第二個按鈕)。我試圖在其中嵌入一些索引(即索引1)來刪除它,但我找不到任何可能的方法;我也失敗了。

這是我試過的方式;

上面給出的移除代碼僅基於整個列表項目計數移除索引項目。我的意思是,例如,第一個元素總是有'0'索引。它不關心我的Index1數據(我可以通過selectedButton作爲sender方法檢索Index1數據)我希望我可以刪除包含用戶指定的Index1數據的按鈕。

我該怎麼做?

在此先感謝。 (Windows Phone 7的)

回答

2

您可以通過添加單擊事件處理程序的按鈕,這樣做:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var button = sender as Button; 
    var item = button.DataContext as CTransaction; 
    if (item != null) 
    { 
     ctransactionList.Remove(item); 
    } 
} 

ctransactionList應該是一個ObservableCollection。

+0

準確地工作! – ilerleartik