Multiple ComboBoxes bound to a common source, enforcing distinct selections

I'm trying to bind multiple ComboBox's to a common source collection and enforce that once a ComboBox selection is made, that selected item is removed from the other ComboBox's as a possibility. The collection is built dynamically, so I'm doing it in code.

I have tried implementing this in several ways so far, and I just can't seem to come up with something that really works.

I've tried using the default view's Filter predicate, but it only passes the item, and I have no way of knowing which control is doing the filter (and it doesn't even make sense conceptually).

I've tried creating new CollectionView's, but the behavior ends up being different (getting SelectionChange events where I didn't before with the default view).

I've been banging my head against this for hours and it just does not seem to want to work. I would appreciate someone more experienced with WPF helping me out with a working example. I would really like it to not auto-select items from the collection and start blank (otherwise, each ComboBox will have a distinct auto-selection which is far too presumptuous).

I'm really close to just allowing broad selection and validate it later, but this seems like such a simple concept to be having such unbelievable difficulty.

Thanks

--------------Solutions-------------

Nice question, i thought about it and i would probably approach it with a MultiBinding and a respective ValueConverter, i.e.

<StackPanel>
<StackPanel.Resources>
<local:ComboBoxItemsSourceFilter x:Key="ComboBoxItemsSourceFilter"/>
</StackPanel.Resources>
<ComboBox Name="cb1">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource ComboBoxItemsSourceFilter}">
<Binding Path="Emps"/> <!-- Source collection binding -->
<Binding ElementName="cb2" Path="SelectedItem"/>
<Binding ElementName="cb3" Path="SelectedItem"/>
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
<ComboBox Name="cb2">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource ComboBoxItemsSourceFilter}">
<Binding Path="Emps"/>
<Binding ElementName="cb1" Path="SelectedItem"/>
<Binding ElementName="cb3" Path="SelectedItem"/>
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
<ComboBox Name="cb3">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource ComboBoxItemsSourceFilter}">
<Binding Path="Emps"/>
<Binding ElementName="cb1" Path="SelectedItem"/>
<Binding ElementName="cb2" Path="SelectedItem"/>
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
</StackPanel>

public class ComboBoxItemsSourceFilter : IMultiValueConverter
{
#region IMultiValueConverter Members

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var collection = new List<object>((object[])values[0]);
foreach (var item in values.Skip(1))
{
if (item != null) collection.Remove(item);
}
return collection;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}

#endregion
}

Since you do this in code behind adding all those bindings should not be much of a problem, just throw all the comboboxes into a list and you can iterate over them. The converter might need some adjustment since it assumes the input collection (values[0]) can be cast to object[].

This way of doing it sadly causes lots of first-chance-exceptions whose cause i could not determine so far...

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in UIAutomationProvider.dll

I'm not exactly sure what I did wrong the first time around, however I did change a few things around and got my original attempt working.

It's possible the problems I was running into were the result of using CollectionView directly, which there is a warning about in the debug output. It states that using CollectionView directly is not fully supported and may have buggy behavior.

I ended up switching to a ListCollectionView. I also created a new List<> containing the contents of my source collection, which allowed me to add a blank object to the list (without changing my original data structure). I simply ignore it on selection change and include it unconditionally in the Filter.

So the steps to making it work:
1) ListCollectionView with source IList
2) Add Filter predicate to the view
3) Create Binding with view as Source
4) combo.SetBinding on ComboBox.ItemsSource
5) On selection changed event, make sure to cast each combo's ItemsSource to an ICollectionView and call Refresh (since it will need to refilter, and property change notifications aren't noticed)

Category:c# Time:2011-04-20 Views:0

Related post

  • WPF ComboBox Bound to Collection of UserControls Not Showing Selected Item's Text 2010-06-22

    I have a ComboBox that is bound to an ObservableCollection of custom UserControls. Each user control has a Tag value set and the ComboBox' DisplayMemberPath is set to "Tag". This correctly displays the Tag of each UserControl in the drop down list wh

  • What to do with multiple projects depending on the same source? 2009-03-19

    This is something I've come across twice in the past month and I'm not even certain how to phrase this as a Google query. I'm actually using SVN for all of this, but it seems like this should be a general versioning problem. We have two projects and

  • C#: wpf adding combobox item to multiple comboboxes 2011-08-07

    I'm trying to set multiple combo boxes to all having the same set of values to choose from (from the code side). ... this.startTimeOptions = value; this.cmbMonday.Items.Clear(); this.cmbTuesday.Items.Clear(); foreach (TimeObject time_option in this.T

  • Updating Multiple ComboBoxes 2011-09-28

    In my project I have about 100 comboboxes, every combobox holds the same items. I want to "update" every combobox like this: When an item is selected in combobox A, all other comboboxes shouldn't hold this item anymore. Likewise, when the selected it

  • Use a My.Setting as Datasource for multiple ComboBoxes 2009-04-09

    I want to maintain a list of potential ComboBox values over multiple executions of my program, and to use that list as the DataSource on multiple ComboBoxes. I also want changes made to the DataSource to propagate over all of the ComboBoxes already e

  • Multiple Comboboxes with a list of objects 2009-08-05

    I have a 3 ComboBoxes in a Form, a list of objects. I need to bind the the comboboxes with 3 different members of the class from the list. (C# 3.0, .NET 3.5) I am currently doing this Title_Combo.DataSource = ListContaining.GroupBy(item => item.Ti

  • multiple comboboxes in R using tcltk 2010-08-14

    I have been trying to define multiple combo boxes in R using the tcltk package but to no avail. I am using the below code. My inspiration was here, however I can't seem to just label them comboBox1, comboBox2, etc... so I decided to try and set their

  • binding multiple ComboBoxes to 1 List 2011-01-03

    I'm trying to bind multiple ComboBoxes to a single List. However when I select a value in 1 combobox, all ComboBoxes that are binded to List change to the same value. List<Country> countryList = new List<Country>(); // Add contries to lis

  • Oracle/SQL Couting multiple colums grouped by a common column 2011-01-26

    I'm back with yet another Oracle query. What I want to do is do counting on multiple columns grouped by a common field. I have half of this done so far. So given the following table THING ACTION -------------- T1 _A_ T1 _A_ T1 _B_ T2 _A_ T2 _B_ I hav

  • How to update a combobox bound in a listbox in WPF? 2012-04-02

    I have a ListBox bound to an ObservableCollection, and inside the ListBox.ItemTemplate I have a ComboBox bound to another ObservableCollection. What I need is to update those ComboBoxes after saving the ListBox data. Here's my XAML: <ListBox Name=

  • Multiple ComboBoxes in a Form 2014-11-13

    I have multiple ComboBoxes in a UserForm, which need to be populated with the same list. The list has been correcly declared, and the following VBA code works OK. For x = LBound(MyList) To UBound(MyList) UserForm1.ComboBox1.AddItem MyList(x) Next I n

  • Combobox displays duplicate items when an item is selected 2011-05-22

    I have a data-bound combobox on a Windows form. When I select an item, all of the other bound components update properly. However, the list of drop-down items changes so that the selected item is duplicated in the list. For example, here is the origi

  • How to make a comboboxed silverlight control which would display in all selected items on the top? 2011-01-28

    I would like to ask, how to make a comboboxed silverlight control which would display in all selected items on the top ? Wtha what I have allready made is a combobox with checkboxes and a specific select logic. That is working fine. But one of the bu

  • How to fill a combobox when a row in a dbgrid is selected? 2011-03-29

    In Delphi 2007 I have a DBGrid. I need to fill a ComboBox when a row in the DBGrid is selected. The data in the the ComboBox depends on the selected row, and can be different for each selected row. The DBGrid contains products orders. When an order i

  • Multiple users working on a common file but different parts 2012-07-21

    Hey all, We would like to build our entire database from inputs coming from 3 different users. Lets say that there are 4 parts of my final database...Part-1 is filled by one user - the 2nd user continues to build this data further by entering more in

  • How to insert 'Empty' field in ComboBox bound to DataTable 2008-10-14

    This is frustrating, as I am sure I have done this before without issue. I have a combo box on a WinForms app in which an item may be selected, but it is not mandatory. I therefore need an 'Empty' first item to indicate that no value has been set. Th

  • C# Update combobox bound to generic list 2009-01-11

    I have a combobox on my form that is bound to a generic list of string like this: private List<string> mAllianceList = new List<string>(); private void FillAllianceList() { // Add alliance name to member alliance list foreach (Village all

  • Managing multiple-processes: What are the common strategies? 2010-08-24

    While multithreading is faster in some cases, sometimes we just want to spawn multiple worker processes to do work. This has the benefits of not crashing the main app if one of the worker crashes, and that the user doesn't need to worry a lot about i

  • Iphone programming: Multiple UITableViews reading from the same source? 2010-11-08

    This question is related to UITableView issue when using separate delegate/dataSource, though I have a different problem. I'm just starting to learn iPhone programming. Basically I have one main view with a table. On the event of a cell click, a sub

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 1.443 (s). 13 q(s)