1

我的应用程序中有列表框。下面是屏幕截图。

在此处输入图像描述

当用户单击列表项时,我将显示详细页面。它正在处理以下选择更改的侦听器。

private void companiesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {            
            //get the selected item from list
            Company selectedItem = (Company)e.AddedItems[0];

     Uri uri = new Uri("/CompanyDetailsPage.xaml", UriKind.Relative);
            //navigate to target page
            this.NavigationService.Navigate(uri);

            FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
            root.DataContext = selectedItem;
}
}

到此为止还好。

现在,当用户单击该项目上的“删除”按钮时,我必须从列表中删除该项目。

private void Del_Btn_clicked(object sender, RoutedEventArgs e)
    {
        //get the Corresponding item from list i.e. On which delete button is placed.
    //Delete saved company from the database   


    }

我无法获得放置删除按钮的特定列表项索引。何我能得到。

谢谢。

4

2 回答 2

2

sender您可以通过转换参数来检索按钮。从那里,您可以通过转换 DataContext 属性来检索公司:

private void Del_Btn_clicked(object sender, RoutedEventArgs e)
{
    var button = (Button)sender;

    var company = (Company)button.DataContext;

    // ...
}
于 2013-07-16T08:39:44.680 回答
0

do get the index of the listbox you can direct set the property

SelectedIndex = {Binding asd,Mode=TwoWay}

then in viewmodel

make a property

private int _asd;
public int asd
{
get
{
return _asd;
}
set
{
_asd= value;
}
}

by this you will get the index of selected item ... hopes it might help ..

于 2013-07-16T08:41:58.080 回答