Suj*_*jiz 4 c# listbox windows-phone-7
我有一个列表框,每行都有删除按钮,所以当我点击删除按钮时,我需要点击列表框的索引,以便删除row.how我可以得到点击项目的索引吗?
这是我的列表框
<ListBox HorizontalAlignment="Left" Name="listBox1" Margin="-3,132,0,0" VerticalAlignment="Top" Width="498" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,1,0,0" BorderBrush="#FFC1BCBC" Width="490">
<Grid Height="70">
<Image Height="50"
HorizontalAlignment="Left"
Name="image"
Stretch="Fill"
VerticalAlignment="Top"
Width="50"
Source="{Binding iconPath}" Margin="8,8,0,0" />
<TextBlock Name="Name" Text="{Binding displayName}" VerticalAlignment="Center" Margin="60,0,0,0" Foreground="Black" FontWeight="SemiBold"></TextBlock>
<Button Name="btnDeleteRow" Width="50" Click="btnDeleteDashboard_Click" Margin="390,0,0,0" BorderBrush="Transparent" Style="{StaticResource logoutbtn_style}">
</Button>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我假设您的ListBox是数据绑定到某些源集合?如果是这种情况,则按钮的DataContext将是您绑定项之一的实例.然后,您可以执行以下操作:
//例如,如果绑定MyDataObject实例列表...
// create a list
List<MyDataObject> myDataObjects = CreateTestData();
// bind it
listBox1.ItemSource = myDataObjects;
...
// in your click handler
private void btnDeleteDashboard_Click(object sender, EventArgs args)
{
// cast the sender to a button
Button button = sender as Button;
// find the item that is the datacontext for this button
MyDataObject dataObject = button.DataContent as MyDataObject;
// get the index
int index = myDataObjects.IndexOf(dataObject);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4394 次 |
| 最近记录: |