views:

823

answers:

2

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!

A: 

Try to set the mode to TwoWay instead of Oneway if you have set so.

Archie
long question; short answer;
echo
My binding is set to TwoWay in my XAML. Any other ideas?
Varn
+1  A: 

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.

Gabe
Thanks Gabe, I'll try this tomorrow and reply back with my results!
Varn
Gabe, your solution is correct! Thanks!
Varn