我的目的是获取listview中所选项目的详细信息。列表视图中的图像和标题通过使用动态创建的堆栈面板显示。动态创建的图像文件和标题将添加到stackpanel。现在我想了解listview中所选项目的详细信息?能不能帮我解决一下
// dynamically creating stackpanel in listview
FileStream stream = new FileStream(item.FullName, FileMode.Open, FileAccess.Read);
Image i = new Image();
i.Width = 100;
i.Height = 100;
i.Margin = new Thickness(15);
i.ToolTip = item.Name;
i.Visibility = Visibility.Visible;
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(SelectedImagePath + "\\" + item.Name);
src.DecodePixelWidth = 100;
src.DecodePixelHeight = 100;
//src.StreamSource = stream;
src.EndInit();
i.Source = src;
Label lb = new Label();
lb.Width = 110;
lb.Height = 110;
lb.Content = item.Name;
StackPanel pn = new StackPanel();
pn.Orientation = Orientation.Vertical;
pn.Width = 160;
pn.Height = 160;
pn.Children.Add(i);
pn.Children.Add(lb);
listView1.Items.Add(pn);
// for retrieving the label control text
string fname;
foreach (StackPanel pan in listView1.SelectedItems)
{
var lbl = pan.Children[1].ToString();//not getting in correct format
MessageBox.Show(lbl.ToString());
}
提前致谢。
答案 0 :(得分:1)
尝试将StackPanel.Children
项投射到其正确的类型,而不是调用ToString()
:
var lbl = (Label)pan.Children[1]; //or use index [0] if you meant the 1st child
MessageBox.Show((string)lbl.Content);