开发者

Binding a combobox's selected item and items list to different properties in wpf

Question: How do you set the binding for a ComboBox's Selected items to one property and the items list to a different property?


Info:

I have an inventory program I'm working on for my company which is aimed mostly at the people who receive shipments as they come in. I have things set up so that there is a list of shipments on one side of the screen. when they select one of these all the information on the shipment is displayed so it can be edited. one bit of information is the person who received the shipment. I want it so that when they click the shipment, obviously the user who received the shipment is the one who pops up in the combo box. but i want the combo box to contain the l开发者_Python百科ist of all the other users.

This wouldn't be to hard if it weren't for the fact that I'm pulling the list of users from a database. the list box has a data template for pulling the proper information out of the list of user data types. what I've tried so far is to have a collection view in the viewmodel that the combo box binds to for its list, then a seperate property which is a single user instance that comes with the shipment data type.

I'm programing in Visual Basic and XAML according to the M-V-VM programing model.


There is nothing wrong with your approach. The ViewModel can expose a collection of types from the db and a separate property bound to the selected item, something like:

public ObservableCollection<Shipment> AllShipments { get; private set; }
public Shipment SelectedShipment { get; set; }

// Constructor
public MyViewModel()
{
    AllShipments = ReadFromDb();
}

In terms of the combobox being empty

  • Have a look in the output window in VS when you start your app - is there a line complaining about a Binding failing?
  • Double-check the spellings of your properties against your xaml Binding
  • Put a breakpoint on the AllShipments getter to make sure it is not null when the View instantiates the binding. If you read the db in the VM's constructor (as above) then this will be OK.

Also - if you want the combo-box to display the User from the shipment object, then remember to ensure your VM (or its base) implements INotifyPropertyChanged, and raises a PropertyChanged event in the SelectedShipment setter - this will propagate the change through to the View via the DataBinding...

hope this helps :)


I'll be answering this question assuming you're using Linq-to-SQL:

This is a common PITA problem, where the User property in your Shipment object contains a reference to a User object that is different from the User object from the query that populated the ComboBox, even though they represent the same record.

To overcome this, you might want to bind the users ComboBox to a list of users that is retrieved from the same DataContext as the list of Shipments.

Example:

class ViewModel
{
    public List<User> Users { get; set; }
    public List<Shipment> Shipments { get; set; }
    public Shipment SelectedShipment { get; set; }

    public void ReadShipments()
    {
        using (var dc = new MyDataContext())
        {
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<Shipment>(x => x.User);
            dc.LoadOptions = dlo;
            Shipments = dc.Shipments.ToList();
            Users = dc.Users.ToList();
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜