使用Linq查询填充组合框

时间:2013-08-19 14:48:42

标签: c# wpf linq visual-studio-2012

我有一个名为“CurrencyOne”的组合框,我写了一个Linq查询

 var curren = (from cur in data.MyCurrencys select cur.Value).ToList();
 CurrencyOne.DataContext = curren;

和xaml fils

<ComboBox Name="CurrencyOne"/>

如何确保组合框将填充查询结果?

2 个答案:

答案 0 :(得分:1)

您正在寻找ItemsSource而不是DataContext

var curren = (from cur in data.MyCurrencys select cur.Value).ToList();
CurrencyOne.ItemsSource = curren;

答案 1 :(得分:0)

您需要将ItemsSource设置为应显示的列表,以便在没有DataContext的情况下直接设置:

var curren = (from cur in data.MyCurrencys select cur.Value).ToList();
CurrencyOne.ItemsSource = curren;

或将ItemsSource绑定到当前DataContext

var curren = (from cur in data.MyCurrencys select cur.Value).ToList();
CurrencyOne.DataContext = curren;

<ComboBox Name="CurrencyOne" ItemsSource="{Binding}"/>