在此列表框中,我显示联系人姓名。获取可绑定集合中物品的索引
<ListBox x:Name="Items" Margin="36,38,78,131">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="lol" Text="{Binding Path=ContactName}" Style="{StaticResource PhoneTextSmallStyle}"
Width="Auto" TextAlignment="Center" FontWeight="Bold" Foreground="White" VerticalAlignment="Bottom" TextWrapping="Wrap"/>
<Button x:Name="ShowName">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="delete" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我得到的接触从本地数据库
public List<FBContacts> listContactDatas { get; set; }
Items = new BindableCollection<FBContacts>();= new BindableCollection<FBContacts>();
public void GetContacts()
{
using(MyDataContext mydb = new MyDataContext(DBConnectionstring))
{
var items = from ContactsList Name in mydb._contacts select Name;
foreach (var toDoItem in items)
{
Items.Add(new FBContacts()
{
ContactName = toDoItem.Name
});
}
}
}
用户可以删除任何接触,如果他按下按钮。
public void delete()
{
Items.RemoveAt(/* index*/);
}
所以如何我可以得到选择联系索引?
更改选择时,SelectedListIndex属性是否更新(例如,Setter上的断点)?也许你需要添加'SelectedIndex =“{Binding SelectedListIndex,Mode = TwoWay}”'强制更新属性 – andreask 2014-09-03 07:59:54
我得到这个错误 System.Reflection.TargetInvocationException“in System.Windows.ni.dll – 2014-09-03 08:07:29
也许列表的选择是在创建SelectedListIndex之前就已经改变了。你如何填充列表?是否通过数据绑定来设置列表项? – andreask 2014-09-03 08:30:50