C# WPF Listview 访问 SelectedItem 和子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1402878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:07:51  来源:igfitidea点击:

WPF Listview Access to SelectedItem and subitems

c#wpflistviewselecteditem

提问by Dave

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

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

<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>

I am filling the listView with the following:

我正在用以下内容填充 listView:

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 });
        }

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?

我有一段时间在双击时从一行中检索数据。DoubleClick 确实会弹出一个消息框,其中包含该行中的所有数据,但我似乎无法只获取一个子项或单元格的数据。假设一行有 ISBN:1234567 标题:Hurrr 作者:Waldo,我如何只检索 ISBN 或仅检索标题?

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

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

仍然是 C# 和 .Net 的新手,并把我的头撞在墙上。我想这应该很简单。

采纳答案by dtb

listView1.SelectedItems[0]returns an object. You first need to cast it to its specific typebefore 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.

listView1.SelectedItems[0]返回一个object. 您首先需要将其转换为特定类型,然后才能访问其成员。对于转换,您需要知道要转换到的类的名称,但是您要向 ListView添加匿名类(= 没有名称)的实例。

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

解决方案:定义一个Book具有 ISBN、Title 和 Author 属性的类(例如),并将 的实例添加Book到 ListView。然后你可以做必要的演员:

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


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

不要忘记将实例 if 添加Book到 ListView 而不是匿名类型的实例:

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);
}

回答by Robert Stepien

    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);
}

I have got an issue with code above, once I use it, my listView did not list any of those values. I do not know if that will help you or if that is correct but after a few tests I added () after "select new Book" and then ListView could show all fields correctly for me.

我遇到了上面代码的问题,一旦我使用它,我的 listView 就没有列出任何这些值。我不知道这是否会对您有所帮助,或者这是否正确,但经过几次测试后,我在“选择新书”之后添加了 (),然后 ListView 可以为我正确显示所有字段。