将列表框项目传递给DoubleClick事件

时间:2016-09-24 10:49:12

标签: c# event-handling

我有这些方法。我可以双击Collection List View中的一个项目,Double_Click方法会触发,我会收到消息框。我无法弄清楚的是如何传递项目本身,以便我可以访问“WWPN”中的字符串值。柱。我想将其作为参数传递给另一个进行搜索的方法。它就像将发送者强制转换回列表项或端口结果一样简单?

  private void BrowseFlogiDatabase_Click(object sender, RoutedEventArgs e)
    {
        CollectionWindow cw = new CollectionWindow(this.VirtualFabricList);

        GridView grid = new GridView();
        GridViewColumn FCPortColumn = new GridViewColumn();
        FCPortColumn.Header = "FC Port";
        FCPortColumn.Width = Double.NaN;
        FCPortColumn.DisplayMemberBinding = new Binding("FCPort");

        GridViewColumn WWPNColumn = new GridViewColumn();
        WWPNColumn.Header = "WWPN";
        WWPNColumn.Width = Double.NaN;
        WWPNColumn.DisplayMemberBinding = new Binding("WWPN");

        GridViewColumn FCIDColumn = new GridViewColumn();
        FCIDColumn.Header = "FCID";
        FCIDColumn.Width = Double.NaN;
        FCIDColumn.DisplayMemberBinding = new Binding("FCID");

        GridViewColumn SwitchNameColumn = new GridViewColumn();
        SwitchNameColumn.Header = "SwitchName";
        SwitchNameColumn.Width = Double.NaN;
        SwitchNameColumn.DisplayMemberBinding = new Binding("SwitchName");

        grid.Columns.Add(FCPortColumn);
        grid.Columns.Add(WWPNColumn);
        grid.Columns.Add(FCIDColumn);
        grid.Columns.Add(SwitchNameColumn);

        cw.CollectionListView.View = grid;
        ObservableCollection<PortResult> flogidatabase = new ObservableCollection<PortResult>();

        var cs = this.switchList.Where(c => c.switchName == this.ProcessedSwitchesListView.SelectedItem.ToString());
        foreach (KeyValuePair<string, FCPort> portpair in cs.FirstOrDefault().FlogiDatabase)

        {
            PortResult pr = new PortResult();
            pr.WWPN = portpair.Key.ToUpper();
            pr.FCPort = portpair.Value.ToString();



            pr.FCID = portpair.Value.WWPNList.Where(w => w.WWPNValue == pr.WWPN).Select(t => t.FCIDValue).FirstOrDefault().ToString();

            pr.SwitchName = cs.FirstOrDefault().switchName;
            flogidatabase.Add(pr);
        }


        cw.CollectionListView.ItemsSource = flogidatabase;

        FlogiAddDoubleClickEventStyle(cw, new MouseButtonEventHandler(FlogiDB_MouseDoubleClick));
        cw.Show();

    }


private void FlogiDB_MouseDoubleClick(Object sender, EventArgs e)
    {


        MessageBox.Show("FlogiDatabase DoubleClick");
    }

1 个答案:

答案 0 :(得分:1)

我想出了答案。我需要访问ListViewItem的DataContext属性。根据上面的代码:

ObservableCollection<PortResult> flogidatabase = new ObservableCollection<PortResult>();
CollectionWindow cw = new CollectionWindow(this.VirtualFabricList);
cw.CollectionListView.ItemsSource = flogidatabase;

然后,为了获得存储在每个ListViewItem中的内容,我们将DataContext转换回PortResult:

PortResult pr = ((PortResult)lvi.DataContext);

然后我们可以访问PortResult中的属性。

string wwpn = pr.WWPN;