如何使用Linq和C#3.5从ComboBox中的SelectedItem中获取值 [英] How do I get values from SelectedItem in ComboBox with Linq and C# 3.5

查看:240
本文介绍了如何使用Linq和C#3.5从ComboBox中的SelectedItem中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的缺少匿名类型的东西,因为我不知道该如何处理Combobox.SelectedItem属性.

I am really missing something with anonymous types, because I can't figure out what to do with the Combobox.SelectedItem property.

这是填充组合框的代码,它很好用

Here's the code that populates the combobox, and it works just fine

         var stocks = from st in brdc.tb_dStocks
                     join su in brdc.tb_rStockUsers on st.StockID equals su.StockID
                     where su.UserID == userRec.UserID
                     select new { st.StockID, su.StockUserID, st.Ticker };

        cboStocks.ItemsSource = stocks;
        cboStocks.DisplayMemberPath = "Ticker";

然后,当有人使用cboStocks组合框选择一个项目时,我需要弄清楚该项目是什么,但我不知道该怎么做.显然,这是一个简单的问题,但极大地困扰了我. cboStocks.SelectedItem是一个对象,并且该对象是由Linq创建的匿名类型,但这就是我能确定的全部内容.

Then, when someone selects an item using the cboStocks combobox I need to figure out what that item is, but I have no idea how to do it. Clearly, this is a simple problem, but its confusing me greatly. cboStocks.SelectedItem is an object, and that object is of the anonymous type created by Linq, but thats all I can figure out.

推荐答案

匿名类型仅对方法真正有用(应仅使用).在这里,您在初始化组合框时用一种方法创建类型,然后在读取所选项目时尝试用另一种方法访问它.这是行不通的.

Anonymous types are only really useful (and should only be used) with a method. Here you're creating the type in one method when you initialise the combo box and then try and access it in another when reading the selected item. This isn't going to work.

您需要创建一个实际类型以分配给组合框的ItemsSource.

You need to create an actual type to assign to the combo box's ItemsSource.

这篇关于如何使用Linq和C#3.5从ComboBox中的SelectedItem中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆