如何将 id 附加到 ListViewItem?

发布于 2024-09-10 05:03:08 字数 1107 浏览 4 评论 0原文

在我最新的项目中,我有一个绑定到 ObservableCollectionListView。此 ObservableCollection 包含我的类 SongData 的许多对象:

public class SongData
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Artist { get; set; }
}

这些对象填充了从 SQLite 数据库派生的数据,并且属性 Id 包含该记录的主键。显然,我不想在我的 ListView 中显示这个 id。但是,当我处理 ListView 上的 DoubleClick 事件时,我确实需要此 id。

我当前的 xaml 代码是:

<ListView Margin="12,41,12,12" Name="lvwOverview" SelectionMode="Single" ItemsSource="{Binding SongCollection}" MouseDoubleClick="lvwOverview_MouseDoubleClick">
     <ListView.View>
        <GridView>
            <GridViewColumn Width="200" Header="Title" DisplayMemberBinding="{Binding Title}"></GridViewColumn>
            <GridViewColumn Width="200" Header="Artist" DisplayMemberBinding="{Binding Artist}"></GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

现在,当用户双击 ListViewItem 时,我希望能够获取 id(用于数据库)。关于如何做到这一点有什么想法吗?

In my latest project, I have a ListView that is bound to an ObservableCollection. This ObservableCollection contains a number of objects of my class SongData:

public class SongData
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Artist { get; set; }
}

These objects are filled with data derived from a SQLite database, and the property Id contains the primary key for that record. Obviously, I don't want to show this id in my ListView. However, I do need this id when I handle a DoubleClick event on the ListView.

My current xaml code is:

<ListView Margin="12,41,12,12" Name="lvwOverview" SelectionMode="Single" ItemsSource="{Binding SongCollection}" MouseDoubleClick="lvwOverview_MouseDoubleClick">
     <ListView.View>
        <GridView>
            <GridViewColumn Width="200" Header="Title" DisplayMemberBinding="{Binding Title}"></GridViewColumn>
            <GridViewColumn Width="200" Header="Artist" DisplayMemberBinding="{Binding Artist}"></GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

Now, I would like to be able to get the id (for use with the database), when the user doubleclicks on a ListViewItem. Any ideas on how to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

川水往事 2024-09-17 05:03:08

尝试

var data = List.SelectedItem as SongData;
if (data != null)
    ...

Try

var data = List.SelectedItem as SongData;
if (data != null)
    ...
早茶月光 2024-09-17 05:03:08

在双击处理程序中,您应该能够获取对双击的项目的引用(或按您的方式找到它)。这与绑定的 SongData 引用相同。

仅仅因为它没有显示并不意味着该属性不再存在。它应该已经准备好并可供使用。

In your double-click handler, you should be able to get a reference to the item that was double-clicked (or work your way to it). That's the same SongData reference that was bound to.

Just because it wasn't shown doesn't mean that the property doesn't exist anymore. It should be there ready and available for use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文