在C#中设置ComboBox的ItemSource和DisplayMemberPath

时间:2012-01-27 09:24:33

标签: c# wpf combobox

我正在通过Tutorial为MEF(Managed Extensibility Framework)为C#工作,但由于这两个代码行(在链接中)无法让exaple-project工作教程可以在界面的大图下找到):

cbTransformationOptions.ItemsSource = _tranformationEngine.Transformers;
cbTransformationOptions.DisplayMemberPath = "Name";

提到的cbTransformationOptions是一个普通的ComboBox。代码无法编译,我收到以下错误:

'System.Windows.Forms.ComboBox' does not contain a definition for 'ItemsSource' 
and no extension method 'ItemsSource' accepting a first argument of type
'System.Windows.Forms.ComboBox' could be found (are you missing a using directive 
or an assembly reference?)

'System.Windows.Forms.ComboBox' does not contain a definition for 'DisplayMemberPath'
and no extension method 'DisplayMemberPath' accepting a first argument of type
'System.Windows.Forms.ComboBox' could be found (are you missing a using directive 
or an assembly reference?)

我现在知道的是,这两个语句都以某种方式链接到WPF / XAML(我在StackOverflow上找到了一些参考文献) - 但我没有任何关于该主题的经验,所以这些答案没有'帮帮我。

如何让代码生效?

2 个答案:

答案 0 :(得分:1)

System.Windows.Forms是创建wpf项目所需的错误命名空间。

答案 1 :(得分:1)

本教程描述的是WPF项目,而您似乎正在尝试针对Windows窗体项目编译代码。

WPF是Windows Forms的替代演示技术,本身就是一个很大的主题。如果您希望构建WPF项目,我建议您购买一本关于WPF的书。

我不知道您在.NET方面的经验如何,但C#2010中的Pro WPF提供了出色的报道。