如何将 id 附加到 ListViewItem?
在我最新的项目中,我有一个绑定到 ObservableCollection
的 ListView
。此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
Try
在双击处理程序中,您应该能够获取对双击的项目的引用(或按您的方式找到它)。这与绑定的 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.