如何绑定查找组合框? [英] How do I bind a lookup combobox?

查看:154
本文介绍了如何绑定查找组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF4组合框下拉列表错误地显示了我的EF4实体的类名。以下是相关的XAML:

 < Window.Resources> 
< CollectionViewSource x:Key =myEntitiesViewSource/>
< /Window.Resources>

< ComboBox ItemsSource ={Binding Source = {StaticResource myEntitiesViewSource}}DisplayMemberPath =CategoryDe​​scriptionSelectedValuePath =CategoryID/>

以下是我的Window_Loaded事件中的代码:

  var categoryList = from p in _context.Categories 
orderby p.CategoryNumber
select p;

System.Windows.Data.CollectionViewSource myEntitiesViewSource =((System.Windows.Data.CollectionViewSource)(this.FindResource(myEntitiesViewSource)));
//通过设置CollectionViewSource.Source属性来加载数据:
myEntitiesViewSource.Source = categoryList;

我的数据库具有一个名为ProjectCategories的连接表的项目和类别之间的多对多关系。类别实体被自动创建为单个实体来表示两个数据库表:



1)包含ID,CategoryDe​​scription和CategoryNumber的类别的查找表和



2)仅包含两个字段的连接表ProjectCategories - Projects and Categories表中的ID。实体模型与我的WPF窗口生活在一个单独的项目中。



我的目标是允许用户从下拉列表中选择一个CategoryDe​​scription,然后单击添加类别按钮将所选类别添加到ProjectCategories的单独列表中。使用当前代码,我在组合框文本区域中看到正确的CategoryDe​​scription,但是下拉列表仅显示实体类名称类别(之前是它的命名空间)!



如何使这个简单的查找组合框正确绑定并显示CategoryDe​​scriptions和CategoryID的SelectedValue的列表?注意:如果可行,我将接受仅在代码中使用的代码,不包括在XAML中的CollectionViewSource。 p>

谢谢!

解决方案

Nevermind。我问了这个问题,自己回答了。我的代码或XAML没有任何错误。 问题是由于使用第三方主题来创建我的控件。一旦我删除了主题,组合框绑定问题就消失了。更多详情看到这篇文章


My WPF4 combobox dropdown list is incorrectly displaying the class name of my EF4 entity. Here is the relevant XAML:

<Window.Resources>
    <CollectionViewSource x:Key="myEntitiesViewSource"/>
</Window.Resources>

<ComboBox ItemsSource="{Binding Source={StaticResource myEntitiesViewSource}}" DisplayMemberPath="CategoryDescription" SelectedValuePath="CategoryID" />

Here is the code in my Window_Loaded event:

var categoryList = from p in _context.Categories
                  orderby p.CategoryNumber
                  select p;

System.Windows.Data.CollectionViewSource myEntitiesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("myEntitiesViewSource")));
// Load data by setting the CollectionViewSource.Source property:
myEntitiesViewSource.Source = categoryList;

My database has a many to many relationship between Projects and Categories with a join table called ProjectCategories. The Categories entity was automagically created as a single entity to represent two database tables:

1) the lookup table of Categories containing an ID, CategoryDescription and CategoryNumber and

2) the join table ProjectCategories containing only two fields - the IDs from the tables Projects and Categories. The entity model lives in a separate project from my WPF window.

My goal is to allow the user to select a CategoryDescription from the dropdown list, then click an Add Category button to add the selected Category to a separate list of ProjectCategories. With the current code I see the correct CategoryDescription in the combobox text area but the dropdown list displays only the entity class name Categories (preceded by it's namespace) multiple times!

How do I make this simple lookup combobox bind correctly and display a list of CategoryDescriptions and a SelectedValue of CategoryID? Note: I'd accept a code only approach leaving out the CollectionViewSource in XAML if it's possible.

Thanks!

解决方案

Nevermind. I asked this question and have answered it myself. There was nothing wrong with my code or XAML. The problem was caused by the use of a third party theme to style my controls. Once I removed the theme the combobox binding problem went away. For more details see this post.

这篇关于如何绑定查找组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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