WPF Listview 访问 SelectedItem 和子项 [英] WPF Listview Access to SelectedItem and subitems

查看:21
本文介绍了WPF Listview 访问 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 作者:Waldo,我如何只检索 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] 返回一个 object.您首先需要将其转换为特定类型,然后才能访问其成员.对于转换,您需要知道要转换到的类的名称,但是您要向 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.

解决方案:定义一个具有 ISBN、Title 和 Author 属性的类(例如,Book)并将 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);
}

<小时>

如果 Book 到 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 Listview 访问 SelectedItem 和子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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