如何通过Silverlight以编程方式设置ComboBox的SelectedItem?(How To Set ComboBox's SelectedItem Programmatically via Silverlight?)

这是我的场景 - 我正在使用SL3(和WCF数据服务),并且我有一个管理员工的自定义表单。 在表单中,我有一些简单的TextBox(es)和一些ComboBox(es)用于输入员工的基本信息。 在我的架构中,我有一些基类,一个用于处理对象集合(实现ObservableCollection和INotifyPropertyChanged),另一个用于实体(捕获和调用OnPropertyChanged(“prop”))。在我的代码后面的文件(.cs) ,我有一个处理组合框_SelectedItemChanged()的事件处理程序,例如EmployeeType,在数据库中,其值可能类似于代码列的Director,Staff,Reporter,Manager等存在于EmployeeType表中,像Description,ModifiedDate和ID)。我在表单控件上有一些构造函数,当我用一个空的构造函数加载表单并因此没有加载任何内容(这是它应该加载和修正的方式)时,一切正常,即我可以输入像FirstName(TextBox),HireData(TextBox)和EmployeeType(ComboBox)这样的数据。我遇到的问题是,当我加载这个表单,并且我知道EmployeeType时,我不知道如何设置ComboBox的选择 编程的dItem?

我尝试了这样的事情,说我想要表单加载EmployeeType作为管理器,所以我在我的Load()方法中:

private SetEmployeeType() { var employeeType = new EmployeeType { Code = "Manager" }; this.EmployeeTypeComboBox.SelectedItem = employeeType; }

但是当我追踪我的代码(F5)时,我将employeeType看作EmployeeType,但是它的属性没有完全填充(除了Code,因为我上面明确称为“Manager”),所以当我的_SelectedItemChanged()事件是调用.SelectedItem = null,因此ComboBox加载没有任何东西(ItemSource绑定到一个列表,它确实通过)。

仅供参考,我有其他方法,我加载我的EmployeeTypes列表,例如GetEmployeeTypes()并加载罚款。 如果ComboBox是空白的,然后我选择一个值,则将正确的值提交给数据库,但正如我注意到的,有时我想要预先定义SelectedItem,并因此禁用ComboBox以禁止用户输入无效数据。

我甚至尝试过一些LINQ,看起来不行:

var type = from e in employeeTypeList // collection loads fine with 10 items where e.Code = "Manager" select e;

当我跟踪上述查询时,'type'会返回正确的EntityType对象,并填充所有的属性(count = 1,这是正确的),但它似乎没有绑定到ComboBox,因为ComboBox的SelectedItemChanged ()期待如下所示:

var employeeType = this.EmployeeType.SelectedItem as EmployeeType; // i.e. expecting EmployeeType

但是相反,我的LINQ查询会带回类似如下的值:

IEnumerable<EmployeeType> // with some extra stuff...

PS。 因为我目前在家,而且这是来自我的工作代码,所以请原谅我,如果我缺少明显的东西,请原谅我。 我尝试了不同的场景,但似乎无法弄清楚。 在此先感谢您的帮助!

Here's my scenario - I am working with SL3 (and WCF Data Services) and I have a custom form that manages Employees. On the form, I have some simple TextBox(es) and some ComboBox(es) for entering basic information for an Employee. In my architecture, I have some base classes, one to handle the Collections of objects (ObservableCollection and INotifyPropertyChanged implemented) and one that is for the Entity (catches and calls OnPropertyChanged("prop"). In my code behind file (.cs), I have an event handler that handles the _SelectedItemChanged() for the ComboBox(es), for example, EmployeeType, where in the database, values might be like Director, Staff, Reporter, Manager, etc. for the Code column (other columns exist in the EmployeeType table like Description, ModifiedDate, and ID). I have some constructors on my form control, and when I load the form with an empty constructor and thus nothing is loaded (which is the way it should load and correct), everything works perfectly, i.e. I can enter data like FirstName (TextBox), HireData (TextBox), and EmployeeType (ComboBox). The issue I am having is when, I am loading this form, and I know the EmployeeType before-hand, so I don't know how to set the ComboBox's SelectedItem programmatically?

I tried something like this, say I want the form to load the EmployeeType as Manager, so I have in my Load() method:

private SetEmployeeType() { var employeeType = new EmployeeType { Code = "Manager" }; this.EmployeeTypeComboBox.SelectedItem = employeeType; }

But as I'm tracing through my code (F5), I see employeeType as an EmployeeType, but it's properties not fully populated (all blank except for Code as I explicitly called "Manager" above), so when my _SelectedItemChanged() event is called, the .SelectedItem = null, and thus the ComboBox is loaded with nothing picked (the ItemSource is bound to a list and it does come through properly).

FYI, I have other methods where I load my list of EmployeeTypes, e.g. GetEmployeeTypes() and that loads fine. If the ComboBox is blank and then I pick a value, the correct value is submitted to the database, but like I noted, sometimes I want to pre-define the SelectedItem and thus disable the ComboBox to disallow the User from entering invalid data.

I even tried some LINQ like so and it seems not to work:

var type = from e in employeeTypeList // collection loads fine with 10 items where e.Code = "Manager" select e;

When I trace through the above query, the 'type' does come back with the correct EntityType object with all of the properties populated (count=1 which is correct), but it doesn't seem to bind to the ComboBox since the ComboBox's SelectedItemChanged() is expecting something like this:

var employeeType = this.EmployeeType.SelectedItem as EmployeeType; // i.e. expecting EmployeeType

but instead, my LINQ query brings back a value of something like:

IEnumerable<EmployeeType> // with some extra stuff...

PS. I am working off from memory since I am currently at home and this is from my code at work, so please excuse me if I am missing something obvious. I have tried different scenarios, and just can't seem to figure it out. Thanks in advance for any assistance!

最满意答案

这听起来像你想把选定的项目设置为经理。

如果是这样,你想要这样的东西:

var type = (from e in employeeTypeList where e.Code = "Manager" select e).FirstOrDefault(); EmployeeType.SelectedItem = type;

您的代码正在创建管理员列表,即使它只有一个项目,也与SelectedItem预期的数据类型不匹配。 解决这个问题的方法是从FirstOrDefault列表中提取列表中的第一个项目,如果列表为空,则返回null。

It sounds like you want to set the selected item to be the manager.

If so, you want something like this:

var type = (from e in employeeTypeList where e.Code = "Manager" select e).FirstOrDefault(); EmployeeType.SelectedItem = type;

Your code is creating a list of managers, which even though it has only one item, does not match the data type expected by SelectedItem. The solution to this is to just extract the one item from the list using FirstOrDefault which will give the first item in the list or null if the list is empty.

如何通过Silverlight以编程方式设置ComboBox的SelectedItem?(How To Set ComboBox's SelectedItem Programmatically via Silverlight?)

这是我的场景 - 我正在使用SL3(和WCF数据服务),并且我有一个管理员工的自定义表单。 在表单中,我有一些简单的TextBox(es)和一些ComboBox(es)用于输入员工的基本信息。 在我的架构中,我有一些基类,一个用于处理对象集合(实现ObservableCollection和INotifyPropertyChanged),另一个用于实体(捕获和调用OnPropertyChanged(“prop”))。在我的代码后面的文件(.cs) ,我有一个处理组合框_SelectedItemChanged()的事件处理程序,例如EmployeeType,在数据库中,其值可能类似于代码列的Director,Staff,Reporter,Manager等存在于EmployeeType表中,像Description,ModifiedDate和ID)。我在表单控件上有一些构造函数,当我用一个空的构造函数加载表单并因此没有加载任何内容(这是它应该加载和修正的方式)时,一切正常,即我可以输入像FirstName(TextBox),HireData(TextBox)和EmployeeType(ComboBox)这样的数据。我遇到的问题是,当我加载这个表单,并且我知道EmployeeType时,我不知道如何设置ComboBox的选择 编程的dItem?

我尝试了这样的事情,说我想要表单加载EmployeeType作为管理器,所以我在我的Load()方法中:

private SetEmployeeType() { var employeeType = new EmployeeType { Code = "Manager" }; this.EmployeeTypeComboBox.SelectedItem = employeeType; }

但是当我追踪我的代码(F5)时,我将employeeType看作EmployeeType,但是它的属性没有完全填充(除了Code,因为我上面明确称为“Manager”),所以当我的_SelectedItemChanged()事件是调用.SelectedItem = null,因此ComboBox加载没有任何东西(ItemSource绑定到一个列表,它确实通过)。

仅供参考,我有其他方法,我加载我的EmployeeTypes列表,例如GetEmployeeTypes()并加载罚款。 如果ComboBox是空白的,然后我选择一个值,则将正确的值提交给数据库,但正如我注意到的,有时我想要预先定义SelectedItem,并因此禁用ComboBox以禁止用户输入无效数据。

我甚至尝试过一些LINQ,看起来不行:

var type = from e in employeeTypeList // collection loads fine with 10 items where e.Code = "Manager" select e;

当我跟踪上述查询时,'type'会返回正确的EntityType对象,并填充所有的属性(count = 1,这是正确的),但它似乎没有绑定到ComboBox,因为ComboBox的SelectedItemChanged ()期待如下所示:

var employeeType = this.EmployeeType.SelectedItem as EmployeeType; // i.e. expecting EmployeeType

但是相反,我的LINQ查询会带回类似如下的值:

IEnumerable<EmployeeType> // with some extra stuff...

PS。 因为我目前在家,而且这是来自我的工作代码,所以请原谅我,如果我缺少明显的东西,请原谅我。 我尝试了不同的场景,但似乎无法弄清楚。 在此先感谢您的帮助!

Here's my scenario - I am working with SL3 (and WCF Data Services) and I have a custom form that manages Employees. On the form, I have some simple TextBox(es) and some ComboBox(es) for entering basic information for an Employee. In my architecture, I have some base classes, one to handle the Collections of objects (ObservableCollection and INotifyPropertyChanged implemented) and one that is for the Entity (catches and calls OnPropertyChanged("prop"). In my code behind file (.cs), I have an event handler that handles the _SelectedItemChanged() for the ComboBox(es), for example, EmployeeType, where in the database, values might be like Director, Staff, Reporter, Manager, etc. for the Code column (other columns exist in the EmployeeType table like Description, ModifiedDate, and ID). I have some constructors on my form control, and when I load the form with an empty constructor and thus nothing is loaded (which is the way it should load and correct), everything works perfectly, i.e. I can enter data like FirstName (TextBox), HireData (TextBox), and EmployeeType (ComboBox). The issue I am having is when, I am loading this form, and I know the EmployeeType before-hand, so I don't know how to set the ComboBox's SelectedItem programmatically?

I tried something like this, say I want the form to load the EmployeeType as Manager, so I have in my Load() method:

private SetEmployeeType() { var employeeType = new EmployeeType { Code = "Manager" }; this.EmployeeTypeComboBox.SelectedItem = employeeType; }

But as I'm tracing through my code (F5), I see employeeType as an EmployeeType, but it's properties not fully populated (all blank except for Code as I explicitly called "Manager" above), so when my _SelectedItemChanged() event is called, the .SelectedItem = null, and thus the ComboBox is loaded with nothing picked (the ItemSource is bound to a list and it does come through properly).

FYI, I have other methods where I load my list of EmployeeTypes, e.g. GetEmployeeTypes() and that loads fine. If the ComboBox is blank and then I pick a value, the correct value is submitted to the database, but like I noted, sometimes I want to pre-define the SelectedItem and thus disable the ComboBox to disallow the User from entering invalid data.

I even tried some LINQ like so and it seems not to work:

var type = from e in employeeTypeList // collection loads fine with 10 items where e.Code = "Manager" select e;

When I trace through the above query, the 'type' does come back with the correct EntityType object with all of the properties populated (count=1 which is correct), but it doesn't seem to bind to the ComboBox since the ComboBox's SelectedItemChanged() is expecting something like this:

var employeeType = this.EmployeeType.SelectedItem as EmployeeType; // i.e. expecting EmployeeType

but instead, my LINQ query brings back a value of something like:

IEnumerable<EmployeeType> // with some extra stuff...

PS. I am working off from memory since I am currently at home and this is from my code at work, so please excuse me if I am missing something obvious. I have tried different scenarios, and just can't seem to figure it out. Thanks in advance for any assistance!

最满意答案

这听起来像你想把选定的项目设置为经理。

如果是这样,你想要这样的东西:

var type = (from e in employeeTypeList where e.Code = "Manager" select e).FirstOrDefault(); EmployeeType.SelectedItem = type;

您的代码正在创建管理员列表,即使它只有一个项目,也与SelectedItem预期的数据类型不匹配。 解决这个问题的方法是从FirstOrDefault列表中提取列表中的第一个项目,如果列表为空,则返回null。

It sounds like you want to set the selected item to be the manager.

If so, you want something like this:

var type = (from e in employeeTypeList where e.Code = "Manager" select e).FirstOrDefault(); EmployeeType.SelectedItem = type;

Your code is creating a list of managers, which even though it has only one item, does not match the data type expected by SelectedItem. The solution to this is to just extract the one item from the list using FirstOrDefault which will give the first item in the list or null if the list is empty.