ComboBox selected item text disappears when scrolling listbox - silverlight

I have Combo boxes inside a list box and whenever the list box is scrolled and the combo box is scrolled off of the screen the list box is firing a selection change event on the combo box and setting the selected index of the combo box to null.
If I scroll back and forth multiple times you will see the selected item display and be removed by scrolling the list back and forth.
Does anyone have an idea on ow to fix this? I need the combo box to retain the selected index.
I have even changed the collection that holds the Combo-box data to a list from an observable collection and it still does the same thing.
I am using silver light v4, .net 4
Thanks...

This is probably a result of the default virtualising nature of the ListBox. As items scroll off the displayed the items are actually removed from the Visual Tree. If you don't have too many items in the list set the ItemsPanel property of the ListBox to an ItemsPanelTemplate containing a simple StackPanel.
Better would be to cease using the selection change event in this scenario, use a binding on the SelectedItem property instead.

I had the same issue, but with a datagrid.
I tried this (preferable solution), but it didnt work for me.
Silverlight ComboBox and SelectedItem
So i had to go with this....
http://forums.silverlight.net/post/396922.aspx

Related

WPF How to programmatically highlight (not select) items in ComboBox dropdown

I have a dropdownlist combobox that uses an ItemTemplate to display information. When the dropdown of the combo is open, I want the user to be able to type things that I will search the data for. Then I'd like to highlight the item (and potentially scroll it into view), but NOT select it (selection in this case is expensive and should only occur when the user presses Enter once he's found the right entry).
Essentially this is how a vanilla combobox behaves and I want to do this for my templated one that searches differently.
I've have an AttachedProperty that does the searching correctly, but I can't figure how to set the highlighted item (IsHighlighted is read-only).
This is not a cosmetic issue only, since Enter should select the highlighted item.
Any ideas?
If you can add a dependency property to the object in the list you are displaying that holds a bool value for the "ShowHighlighted" state, you could then add a trigger to your ItemTemplate that changes the Background Brush based upon the value of ShowHighlighted.

Silverlight ListBox Items beeing displayed in a different order thant the Items collection

I have a ListBox in a Silverlight Application. I'm trying to make an editable listbox, so I use an ItemTemplate to have the controls i need in each item, like a textBox and buttons, and its working fine.
I'd like to have a line at the end of the Listbox with a button to add new items. Since this item won't be related to any of my domain classes, I'm using a plain object as a 'Filler', and then I have code that identify this item to show the button correctly.
myListBox.Items.add(new object());
The problem is that I want this "new Record" item to be kept always at the end of listbox, so when I need to insert a new domain record, i use this code:
myListBox.Items.Insert(myListBox.Items.Count - 1, domainItem);
When I debug the myListBox.Items collections, it is in the right order, with the "add new" button at the end, but the listbox is displaying this button at the beggining. Why are my items beeing displayed in a different order than the Items collection?
Unless there is a specific reason, instead of trying to put the button into the listbox collection itself you would be best to create a new control with the button outside and below the listbox. You can always style the button to look as though it's within the listbox if required.
Is there a reason why you are not using a datagrid because it would remove all your ordering problems and it would allow you to edit the entries.
The datagrid is bound to an ObservableCollection which automatically connects your editable fields to the GUI.
Cheers,

WPF popup on ListBoxItem

I have a MiniToolbar popup that shows up at Mouseover on a ListBoxItem, it needs to show just under the item.
(a MouseOver trigger also sets the IsSelected property on the items)
I tried two options :
define the popup on the items DataTemplate
define the popup on the ControlTemplate for the ListBoxItem
Both options work fine, however I was wondering if the popup was recreated each time ??
(please advise)
I think it would be better to define the popup in the ControlTemplate of the containing ListBox rather than the ListBoxItem ?
I tried this, but could not find the binding expression for placement property relative to the SelectedItem (it shows up at the bottom of the ListBox, not bottom of ListBoxItem).
Any suggestions ?
thanks in advance.
Michael.
The popup is created one time for each list box item in both cases.
I would not suggest that you use single popup for all items in the CotnrolTemplate for the list box because it significantly complicates things. But if you still want to do so, you can set Placement="Custom" on you popup and specify CustomPopupPlacementCallback. In that callback you can calculate the placement using the position of currently selected item.

silverlight 3 listbox item highlight versus selected

I have a listbox and am attempting to select and item in code. Sometime one item is highlighted, that is it is background is colored blue, but a different item has a square blue box around the it (no highlighting just an hollow outline of a box).
Am I correct in saying one is "highlighted" and one is "selected" and do I have them correctly identified?
Should this be happening... that is these 2 things being out of sync?
Thanks
Cody
Just fought this issue.
Although the listbox scrollviewer will auto scroll too the selected item in the listbox the first item would have focus and as you discribe keyboard interaction then operated with the first item in the list not eh selected item.
For us the fix was
this.MyListBox.UpdateLayout();
this.MyListBox.Focus();
this.MyListBox.SelectedItem = MyObject;
this.MyListBox.ScrollIntoView(this.MyListBox.SelectedItem);
The order of the actions seems very important.
This was using the ListBox internal scrollviewer.
The item with the blue highlighted background is the SelectedItem. The item with the blue rectangle is the item that currently believes it has the focus.
Ordinarily the Focus rectangle the selected fill are found together because the selected item usually changes with a mouse click which also brings the focus to the same element. However its possible for example that code may change the selected item whilst the ListBox still has the focus. In that case the selected highlight will move the newly selected item but the focus rectangle will remain where it is. (Note to the pendatic I'm describing what appears to the user not how things actually work under the hood).
For an insight on what is going on see the ListBoxItem style in the ListBox Styles and Templates documentation.

Setting dropdown height of a combobox in WPF

I have a combobox in WPF that I add items to at run-time (via a data binding to a List). I'd like to set the height of the dropdown box dynamically so that all (or most) of the items show. Unfortunately it seems that the height of the dropdown is set once and cannot be dynamically altered. It always seems to be the same size.
Is there some relatively straightforward to adjust the dropdown height?
Yes, I've tried setting it after adding the items to the combobox. In the debugger it looks like the new value is there. However, when I open the dropdown, it drops down to a size of its own choosing. I've tried exaggerating the MaxDropDownHeight and have even set it to "Auto" to no avail.
The property MaxDropDownHeight is a dependency property, on the combo box that controls the height of the drop down list. Since it's a dependency property, it's value can be set dynamically.
Have you tried setting this value?
MaxDropDownHeight="Auto"
For me, the issue was that ComboBox dropdown had height of 95 pixels regardles of the amount of items.
My data source for the ComboBox was a Collection<>, but after changing it to ObservableCollection<>, the ComboBox dropdown opens showing all items.
Now also the MaxDropDownHeight property works just fine.
Ref: This comment in here Make the dropdown of a combobox be shorter?
Set your ComboBox's Style={x:Null} to ensure that you don't have a style issue affecting the drop-down panel size.

Resources