WPF列表视图访问的SelectedItem和子项目 [英] WPF Listview Access to SelectedItem and subitems

查看:290
本文介绍了WPF列表视图访问的SelectedItem和子项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有更多的问题,我的C#WPF ListView控件。在这里,它是在其所有的荣耀:

Ok, I am having more problems with my C# WPF ListView control. Here it is in all its glory:

<Window x:Class="ebook.SearchResults" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="ISBNListView" Height="503" Width="1004">
<Grid>
    <ListView Name="listView1" Margin"22,30,33,28" MouseDoubleClick="getSelectedItem" >

        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="ISBN" Width="150" DisplayMemberBinding="{Binding ISBN}"/>
                    <GridViewColumn Header="Title" Width="350" DisplayMemberBinding="{Binding Title}"/>
                    <GridViewColumn Header="Author" Width="350" DisplayMemberBinding="{Binding Author}" />
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

我填充ListView控件与以下内容:

I am filling the listView with the following:

XDocument xdoc = XDocument.Load(GlobalVars.docPath + "\\tempSearchResults.xml");
        var items = from item in xdoc.Descendants("Book")
                    select new
                    {
                        ISBN = item.Element("ISBN").Value,
                        Title = item.Element("Title").Value,
                        AuthTexts = item.Element("Author").Value
                    };
        foreach (var item in items)
        {
            listView1.Items.Add(new { ISBN = item.ISBN, Title = item.Title, Author = item.AuthTexts });
        }

我有一个时间从行检索数据时,它被双击一个魔鬼。
在DoubleClick并弹出与行中的所有数据一个消息框,我似乎无法得到只有一个子项或单元格的数据。说行有ISBN:1234567标题:Hurrr作者:瓦尔多,我怎么只检索ISBN或只是标题

I am having a devil of a time retrieving data from a row when it is double clicked. The DoubleClick does popup a message box with all the data in the row, I just cannot seem to get only one subitem or cell's data. Say a row has ISBN: 1234567 Title: Hurrr Author: Waldo, how do I just retrieve the ISBN or just the title?

private void getSelectedItem(object sender, MouseButtonEventArgs e)
    {
        System.Windows.MessageBox.Show(listView1.SelectedItems[0].ToString());
    }

不过新C#和.NET和敲我的头撞在墙上。我想这应该是相当简单的。

Still new to C# and .Net and banging my head against the wall. I figure this should be rather simple.

推荐答案

listView1.SelectedItems [0] 返回对象。首先,您需要为将它转换为它的特定类型之前,您可以访问它的成员。铸造你需要知道类投来的名字,但你添加的匿名类(=没有名称)的情况下,你的ListView。

listView1.SelectedItems[0] returns an object. You first need to cast it to its specific type before you can access its members. For casting you need to know the name of the class to cast to, but you're adding instances of an anonymous class (= has no name) to your ListView.

解决方案:定义一个类(例如,图书)使用,书名和作者的属性,并添加 Book的实例到ListView。然后,你可以做必要的转换:

Solution: Define a class (e.g., Book) with ISBN, Title and Author properties and add instances of Book to the ListView. Then you can do the necessary cast:

private void getSelectedItem(object sender, MouseButtonEventArgs e)
{
    Book book = (Book)listView1.SelectedItems[0];
    System.Windows.MessageBox.Show(book.ISBN);
}



不要忘了添加实例,如果图书到ListView,而不是一个匿名类型的实例:

Don't forget to add instances if Book to the ListView instead of instances of an anonymous type:

var items = from item in xdoc.Descendants("Book")
            select new Book                                   //  <---
            {
                ISBN = (string)item.Element("ISBN"),
                Title = (string)item.Element("Title"),
                Author = (string)item.Element("Author"),
            };

foreach (var item in items)
{
    listView1.Items.Add(item);
}

这篇关于WPF列表视图访问的SelectedItem和子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆