简体   繁体   中英

Get selected Item details from a data binded List in C#

I have to access the details of the selectedItem from the List which was binded earlier. when I display the SelectedItem, what I get is the datacontextName.TableName . How do I access the value of selected Item?

My XAML Code is:

<TabItem Header="Playlist">
    <ListView Name="RecList" SelectionMode="Single" SelectionChanged="RecList_SelectionChanged"  >
        <!--2 data from table tblMusic, Name and Playtime, is binded-->
        <ListView.View>
             <GridView>
                 <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                 <GridViewColumn Header="Time" DisplayMemberBinding="{Binding Playtime}"/>
             </GridView>
        </ListView.View>
    </ListView>
</TabItem>

And the C# fucntions are:

public MainWindow()
{
MusicDataClassDataContext dc = new MusicDataClassDataContext(Properties.Settings.Default.EliseDBConnectionString);
 //binding of the List.
    if (dc.DatabaseExists())
     {
         RecList.ItemsSource = dc.tblMusics.ToList();
     }
}

private void RecList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //I tried both SelectedItem and SelectedValue
    MessageBox.Show(RecList.SelectedValue.ToString());
}

Is it possible to get other details from the binded table using this List?

It was a casting Problem. this works fine for me:

The selectedItem should be casted to the type : tblMusic (the data type of the table) and store it to a variable. That variable can be used to access all the details of the table that was binded before.

tblMusic Mus = (tblMusic)RecList.SelectedItem;
MessageBox.Show(Mus.Name);

This gives the details of the table that was binded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM