我的列表框包含垂直列出的按钮,下面给出了相关数据;
<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的数据绑定,它使用任意整数索引该按钮。我希望用户使用此代码删除指定的索引按钮;
ctransactionList.RemoveAt("index comes here");
我的确想要删除点击的按钮,(如果点击第二个按钮,则删除第二个按钮)。我试图在其中嵌入一些索引(即Index1)来删除它,我找不到任何可能的方法;我也失败了。
这是我尝试的方式;
上面给出的删除代码仅根据整个列表项计数删除索引项。我的意思是,例如,第一个元素总是具有'0'索引。它不关心我的Index1数据。(我可以使用selectedButton作为发送方法检索Index1数据)我希望我可以删除包含用户指定的Index1数据的按钮。
我该怎么做?
提前致谢。 (Windows Phone 7)
答案 0 :(得分: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。