Listbox with checkboxes - code seeing every item IsChecked = true - c#

I have a WPF Listbox which contains a list of checkboxes which are all named as the names of other controls in another window.
When the listbox is looped by grabbing each item in lst_control.Items:
_details.controlIDs.Clear();
foreach(Control item in lst_controls.Items)
{
if (item.IsChecked)
//Add item to list
_details.controlIDs.Add(item.controlID);
}
The code is fired on a check/uncheck of any of the checkboxes within the listbox. It sees each item.IsChecked as true - even if it is unchecked.
EG: Check the top box in the list, it sees it as IsChecked = true, but it also does for every other control in the list.
Weird behavior - has anyone seen this before?

Even if you're not going to go the full MVVM route, this issue is best resolved by separating the UI and the data layers in your application.
Make a data item class that includes an IsSelected boolean property, and set the ListBox's ItemsSource to a collection (eg ObservableCollection) of these items. For two way data binding, the data item class should implement INotifyPropertyChanged.
In your UI, make a DataTemplate for the ListBox's ItemTemplate property, that includes a CheckBox that is bound to IsSelected.
That way you can scan the collection of data items (using Linq or otherwise) to find those that are selected.

Related

Maintaining combobox text after selecting a bound item in WPF / C#

I have a WPF ComboBox which I'm using to search names. I use the MVVM pattern and it's all pretty simple:
The ComboBox Text property is bound to a "SearchString" property on the VM. The ComboBox ItemsSource is bound to a "SearchResult" property on the VM. This is a list of objects displayed using a DataTemplate. The ComboBox also triggers a "PreviewKeyDown" event and pressing enter selects the first item in the result set, with up and down arrow keys traversing the results. The SelectedItem is bound to the DataContext for a GroupBox. This part works really nicely.
When an item is selected in the ComboBox, WPF is automatically attempting to replace "Text" with my SelectedItem. This causes my results set to get emptied and "Text" to revert to an empty string.
The behaviour I'd like is that when an item is selected, the text in the ComboBox remains exactly the same, so that my user can continue to traverse the result set using up and down arrows.
Is there an elegant way to achieve this? I think that nothing will be added to the question with a code snippet, but happy to supply if wanted.
The behaviour I'd like is that when an item is selected, the text in the ComboBox remains exactly the same, so that my user can continue to traverse the result set using up and down arrows.
That's a requirement that's not built into the ComboBox control. If you want something like that, you'll have to build it yourself (and subclassing most likely won't help, you'll have to build most of a ComboBox from scratch for this).
In fact you shouldn't be binding the current item like that either, you should use the SelectedItem property, which doesn't do any conversions or copying, exactly what you want for an internal binding.

Track changes inside the collection of complex objects

I have a collection of objects "SourceItemCollection" used to populate the ListBox with checkboxes. Each item of the collection consists of two fields, Item and IsChecked (I've created a small class for this combined items). I would like to track all changes when user selects or deselects something from the collection (not when the button is pressed or something else, but "on-sight"). For this I would like to use another collection "SelectedItems" which will consist only of Items, without IsChecked property (I create and would like to use this collection outside the abovementioned small class of the source collection's items).
The tricky thing is that "SourceItemCollection" doesn't change itself, it always stays the same, changes only the IsChecked property of each item. I do get a notification each time I tick or untick something, but I get it inside the small class of my combined items and I can't access my SelectedItems collection from there.

Pull data from a datagrid and display them on checkboxes within a combobox in WPF

Please am new to WPF and am creating an application in which a user can select multiple items via checkboxes within a combobox. The items on the checkboxes are subject to change so, i want to add an editable datagrid table. Please, my problem now is to make any changes on the datagrid be reflected by the checkboxes.
I was only to add the checkboxes within the combox with xaml and have finished with the styling but don't know how to go about the codes.
Seems to me like what you need is to bind the list of items from the DataGrid to the ComboBox (let them have binding to the same DataContext), and set the ComboBox's ItemContainerStyle to display a CheckBox.
I don't fully understand what the check of the CheckBox should display, but perhaps it can be data bound to some other property or to the data item itself and use a converter, or it can look at a property of the data item and use Triggers or DataTriggers.
Perhaps if you posted some code we could give a better answer...
Hope it helps.

Silverlight: How to databind on a condition

I have an ItemsControl object and am setting the DataTemplate to hold a Grid with a couple controls in it. The controls are databoud to a collection of some object MyObj, specifically a TextBlock and a ComboBox. MyObj has its own collection inside of it for a property. If that property has only 1 object in its collection, only the TextBlock is visible. But if there is more than 1 object in the collection, the TextBlock is visible and the ComboBox becomes visible once the TextBlock is clicked on.
I have the ComboBox filled up with what it needs, I just can't figure out how to specify which ComboBox needs to become visible when the TextBlock is Clicked on.
I guess my question is, how would I even go about doing this? Or, is there a better way to think about this problem?
I'm new to databinding in Silverlight and running into a bunch of issues on my own. Any help is always appreciated. Thank in advance.
One thing that you could do is add an extra property to the data item that you binding to, something like 'IsSelectionAvailable'. Make the visibility of your combobox bound to this property (via a boolean to Visibility enum Value Converter). Finally, add a click event handler for the text box that sets the IsSelectionAvailable property to true for the object it is bound to.
Hope that helps.

How do I get Count of parent collection from child?

I've got an ObservableCollection assigned to ItemsSource of a listbox. Listbox is using a DataTemplate which has a usercontrol in it which has items bound to each listboxitem's properties.
I have an up and down button on the usercontrol which moves an item up or down the list. The list is sorted by the property that I'm changing. Click up or down, the DisplayOrder property is changed, I'm using INotifyProperty to tell the ObservableCollection it needs to re-sort.
What is the best way for the usercontrol to get the item count so that I can disable the down button when an item reaches the bottom of the list. (The top is easy, I compare to 0)
I see two ways of handling this.
The first is to pass a handle of your collection to each of your items (when they get added to the collection) so that they can calculate if they are the first or last item themselves.
The other is to expose writable properties on your items, such as CanGoUp and CanGoDown, and your parent control becomes responsible for setting these properties properly. I prefer this solution because it decouples the behavior of your parent list, from the child items. Even though the up/down buttons are placed on your child items, it's really a functionality of the parent list.
listBox1.Items.Count ?
this.Parent.Controls.Count?

Categories

Resources