我有这些方法。我可以双击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");
}
我找到了答案。我需要访问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;