views:

29

answers:

2

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?

A: 

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.

casperOne
A: 

Try

var data = List.SelectedItem as SongData;
if (data != null)
    ...
STO
This works, thanks! Weird that I didn't think of this myself :D
Landervdb