質問

Here's my problem: I need to make a DataGrid with dynamic comboboxes using the WPF. If the value of a combobox is already used in the previous rows, the next ones, that will be added by the user, shouldn't contain the item already used.

In this image, the ITEM A shouldn't apear on the combobox of the second line.

I don't have ideia how to accomplish this, can anyone show me a light?

OBS: The DataGrid ItemsSource is binded to an ObservableCollection, and the DataGridComboBoxColumn ItemsSource is a List.

Thanks !!!

役に立ちましたか?

解決

The ItemsSource of the combo doesn't have to be bound to an ObservableCollection, but it can help depending on exactly how you solve this.

When that cell goes in to edit mode the property the ItemsSource is bound to gets hit - so you can return a new list of items each time the getter is hit. Here is a very basic example to give you an idea:

public List<string> MyItemsSource
{
    get 
    {
        var myNewList = MyMasterList.ToList(); //create a (reference) copy of the master list (the items are not copied though, they remain the same in both lists)
        if (PropertyA != null)
            myNewList.Remove(PropertyA);

        return myNewList;
    }
}

So what you are creating and returning is a filtered version of your master list of all possible items. LINQ will be of great help to you here.

Alternatively you could keep just one static copy of the master list as an ObservableCollection, and simply remove items from that static copy as they get selected (and add them back in as they get unselected). Which option you choose will depend on how many times the list can be modified due to items being selected and how complicated it is to generate the list. I've used the dynamically generated list many times in the past, it's an option that works well in most cases.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top