如何在wpf ComboBox中选择DisplayMemberPath显示的项目?

时间:2015-10-16 17:11:36

标签: c# wpf combobox

我有一个 ComboBox 控件,我从 Web服务 获取控件的 ItemsSource (Asp.Net WebApi) 2),其代码如下:

cmb_Class.ItemsSource = from c in Container.Classes select new {ClassID = c.Class_Id};
cmb_Class.DisplayMemberPath = "ClassID";

我曾使用DisplayMemberPath,因此我只能获得指定的值。

但是,

问题在于,当我从下拉菜单中选择项目时,整个字符串将在Combobox中显示为所选项目,而不是该项目。

为了更好地理解我的问题的形象如下:

enter image description here

请告诉我如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

我的问题已经解决了。

我使用 ExtendedWpfToolkit WatermarkCombobox控件,当我将其更改为默认的wpf Combobox控件时,我的问题就解决了。

答案 1 :(得分:0)

只需为WatermarkComboBox添加实际解决方案。似乎WatermarkComboBox缺少XAML的一部分。

我会为这个带有Blend的ComboBox创建一个样式,然后在/在行内:

<ContentPresenter x:Name="ContentSite" 

添加以下XAML行:

ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"

这将解决问题。

相关问题