Reputation: 115
I have listbox which contains buttons listed vertically with related datas as given below;
<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>
And I'm binding the data with such classes;
[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;
}
}
}
As shown above, I have a data binding named Index1 which indexes the button with an arbitrary integer. I want to remove a specified indexed button by user with this code;
ctransactionList.RemoveAt("index comes here");
My exact desire is to remove the clicked button,(i.e. if the second button clicked, then remove the second one). And I tried to embed some indexes(i.e. Index1) in it to remove it, I couldn't find any possible way; I also failed again.
This is the way I tried;
The removal code given above only removes the indexed item based on the whole list item count. I mean, for example, the first element always has the '0' index. It does not care my Index1 data.(I can retrieve the Index1 data with selectedButton as sender method) I wish I could have removed the button which includes the user-specified Index1 data.
How can I do this?
Thanks in advance. (Windows Phone 7)
Upvotes: 0
Views: 225
Reputation: 547
You can do this by adding click event handler for button:
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 should be an ObservableCollection.
Upvotes: 2