[英]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. 我必须从先前绑定的列表中访问selectedItem的详细信息。 when I display the SelectedItem, what I get is the datacontextName.TableName .
当我显示SelectedItem时,得到的是datacontextName.TableName 。 How do I access the value of selected Item?
如何访问所选商品的价值?
My XAML Code is: 我的XAML代码是:
<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: C#功能是:
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. selectedItem应该转换为类型: tblMusic (表的数据类型),并将其存储到变量中。 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. 这提供了绑定表的详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.