I have several ComboBoxes in a WinForms application (written in C++/CLI) that I would like to bind to the same data source, a List of strings. Let's say for simplicity's sake that there are 2 ComboBoxes and the List has 4 elements, "Object 1", "Object 2", "Object 3", and "Object 4".
I'd like to have the selections work in the following way:
I select "Object 1" from ComboBox1.
I go to select other objects from ComboBox2. However, since "Object 1" has already been selected in ComboBox1, the only options I see in the drop-down menu from ComboBox2 are "Object 2", "Object 3", and "Object 4".
Basically, the selection in each ComboBox should be unique.
I think this can be done using CollectionView's in WPF, but how can I do this in WinForms?
Thanks for your help.
Try handle BindingComplete event for each Combobox's binding and inside it set the other Comboboxes DataSource property(filter or whatever). Make sure you set FormattingEnabled property of Binding to true to enable the BindingComplete event to be raised.
Related
So I have this combobox, which ItemsSource is set to a List of objects.
What I want to do after that is change the Display value of one specific Item (the first on the list), because this label does not suit the context in some situations but does in others.
So far I found that you could add an item using the Add method, insert one using the Insert method, and remove one using RemoveAt.
But how do you update one ? I know I could use RemoveAt and Insert afterwards, but it would be a bit labor intensive, plus I would have to recreate the object with all its values...
Any ideas?
Either replace an item in your "List of objects":
myList[0] = new MyObject("Another display text").
For this (and Add, Remove etc) to work, your list would have to be an ObservableCollection<T> or any other that implements INotifyCollectionChanged.
Or modify the item itself:
myList[0].DisplayText = "Another display text";
For this to work, your MyObject class would have to implement INotifyPropertyChanged, you'd have to raise the PropertyChanged event when the DisplayText property is set, and you'd have to set the DisplayMemberPath or the ItemTemplate of your ComboBox correctly.
Take a look at the MVVM pattern for more information.
I am using a ToolStripControlHost to popup various other controls such as datagridviews, listviews, etc.. How do you assign a datasource to a combobox that is hosted in this manner. Setting the datasource using dataview, datatable, etc does not work. Does anyone know the secret or is this impossible?
The ToolStripComboBox does not support data binding so you'll need to add the items by hand. Thankfully, the combo box has a name on the form so you can just do this:
toolStripComboBox.Items.AddRange(
new object[]
{
"Value 1",
"Value 2",
etc.
}
I don't know what you named the combo box, but just put that name in place of toolStripComboBox.
Update for ComboBox Property
If you wanted to bind via the ComboBox property then you should be able to do something like this:
var cb = toolStripComboBox.ComboBox;
cb.ValueMember = "some field or property";
cb.DisplayMember = "some field or property";
cb.DataSource = {some IEnumerable<T> or DataView or some other sort of bindable list}
i have a datagrid bound to a property. In this grid i have columns which consists of cells which are like hyperlink i mean when user clicks on the cell value based on these values another gird will get populated. i want to know how to get the cell value and pass it to some method so that other grid will get populated.
The best way to do this is in your viewmodel.
You should bind the SelectedItem of your datagrid to a new property in your ViewModel. In the set method of this new Property, call a new method to populate a new ObservableCollection/List/whatever...
Finally, bind your "other grid" ItemsSource to this new observable collection from your ViewModel.
Edit:
If you need to load one thing or another depending on the column you are going to use the code behind, take a look at this:
Silverlight DataGrid how to get cell value from a selected item?
I have a listbox bound to a list of business objects. The items in the listbox are formatted using an itemtemplate. The itemtemplate includes a checkbox bound to a boolean property of the business object. When I spin up the app, the bool prop on the object in the list is changed when I click the checkbox. so far, so good.
The dialog has "select all" and "clear all" buttons. When I click on of these buttons, the properties on the objects are changed but the checkbox does not update.
The code in the select all click event is. . .
For Each x As BusObj In _BusObjList
x.BlockIsInserted = True
Next
I can step through the code and watch the object properties change but the checkbox does not update. Any suggestions?
Thanks,
using twoway binding should help I guess
{Binding ..., Path=Text, Mode=TwoWay}
And yes, is BlockIsInserted property dependency? or implemented INotifyPropertyChanged?
I've encountered the same issue, even with the binding set to two-way and the view model representing the business object correctly implementing INotifyPropertyChanged. The (rather brute-force) solution I found was to NotifyChanged on the property representing the collection of business objects - this fixes the problem.
I have two viewmodels. One which displays a collection of IPAddresses, and one which displays a collection of objects that has numerous parameters. One of these parameters is an IPAddress. So, I have another panel that binds to the properties of the second object. I would like a combobox to have the ItemSource set to the first object, but the selected item bound to the second object. However, I can only seem to set one datacontext on a control in code behind. Is there any way around this? I would prefer to do this all in code behind if possible (i find the xaml programming to be non-ideal at best), but I'll take anything.
For the ComboBox bind the collection of IPAddresses to the ItemsSource property, and bind the SelectedItem of the ComboBox to the IPAddress property of the SelectedItem of the collection of "numerous property objects".
This would be easier to answer if I had a better description of your objects including names. But it sounds to me like you should make a dictionary with the ipaddress as the key and the second object as the value.
If you can do that then you can bind to it in code like so:
comboBox.ItemsSource = dictionary;
comboBox.DisplayMemberPath = "Key";
comboBox.SelectedValuePath = "Value";
This is assuming that you have exactly one "second object" for every IPAddress in the collection. Which sounds about right given your description.
Take a look at the Source property of Bindings. It is basically a DataContext for a specific binding. It should make what you're trying to do very simple, especially in code behind.