我有一个列表框中的一个简单的项目列表。在我的XAML页面,我有以下silverlight/windows phone 7 selectedIndex问题列表框中的按钮
<ListBox Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding firstName}"/>
<TextBlock Text="{Binding lastName}"/>
<Button BorderThickness="0" Click="buttonPerson_Click">
<Image Source="delete-icon.png"/>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在我隐藏,我试图抓住将selectedIndex,所以我可以从绑定到我的列表框集合中删除的项目。
private void buttonPerson_Click(object sender, RoutedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (listBox1.SelectedIndex == -1)
return;
myPersonList.removeAt(listBox1.SelectedIndex);
}
然而,无论在哪一行我点击删除按钮,的selectedIndex始终是-1
我缺少什么?
在此先感谢!
这个答案已经相当有帮助的。如果我不想将整个对象附加到标签怎么办?如果我只是想附加一个数字..说... selectedIndex? – Dave 2010-11-16 08:57:54
我会避免使用索引(因为它更难做,而且代码更改时灵活性更低)。您可以添加一个ID属性(独特的东西)到您的自定义对象,并将标签绑定到此。然后,您可以循环访问列表并根据此ID删除,或者使用密钥设置为该ID的字典。 – theChrisKent 2010-11-16 14:20:56