简体   繁体   English

从C#中的数据绑定列表中获取选定的项目详细信息

[英]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.

相关问题 c#WPF从绑定到类对象的List &lt;&gt;获取值 - c# WPF get value from a List<> that is binded to a class object 如何从下拉列表中通过C#中的绑定绑定通过jQuery获取选定的文本 - how to get selected text from dropdown binded trhough jquery in c# c#-如何从模型中的项目列表中获取特定项目,并在单击按钮时显示所选项目的属性? - c# - How to get a specific item from a list of items in model and display the properties of the selected item on button click? 如何使用c#从livelink获取工作流项目详细信息? - How to get the workflow item details from livelink using c#? 从WPF和C#中的选定项目中获取listView项目编号 - Get listView item number from selected item in wpf and c# 如何从html下拉列表中获取所选项目并将其传递给mvc c#中的另一个控制器 - how to get the selected item from html dropdown list and pass it to another controller in mvc c# 获取绑定列表框中所选项目的值 - Get value of selected item in binded listbox 如何将列表框中的选定项目添加到列表 <string> 在C#中 - how to add the selected item from a listbox to list <string> in c# C#从不同的弹出窗体列表中计算选定的项目 - C# Calculate selected item from list of different popup forms 如何从C#中的列表或var中获取随机数量的详细信息 - how to get random amount of details from a list or var in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM