A strange effect when click some space in ListViewItem - wpf

My ListView's ItemTemplate is a very complex Grid, which has multiple rows and columns. The ListView's SelectionMode is Extended.
I met a very strange effect when I click my ListViewItem now. When I click some specific space, such as two gridrows' gap, the item isn't been selected, the ListView will scroll up, then the mouse pointed the next item, no item is selected. This effect only occurs when I click the last item in the view of the ListView.
The ListViewItem's IsSelected property is bound to my model's IsSelected property, and the binding mode is two way. My ItemsSource is ObservableCollection testList, I also have a ObservableCollection which contains the Items which are selected. If double click the item, a new window will be open which display the details of the selected model.
Sometimes, when I single click the space which I mentioned, it even has effect of double click, but an exception is thrown, because more than one item in the selected collection.
Anybody met the strange problem?

Related

WPF Listbox add item without scrolling

Given a WPF ListBox with a vertical scrollbar that uses databinding, is there a way to get it to not scroll when items are added to the bound collection?
Use case: User is scrolled down the list to an item and is reading it. New item is added to the head of the bound collection. Listbox should not scroll or otherwise "jump".
Currently, when I add an item to the head of the collection, the listbox scrolls the text down one item.

ComboBox selected item text disappears when scrolling listbox

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

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.

How to handle the event when click on empty space?

I want to catch the event when a listview is left-clicked on an empty space - i.e. clicking on no item within the listview control.
I've search in the Events list of the listview but found none. How can I do this?
Please help!
[Edit]
What I want to do if I could catch this event: Deselect all items in the listview.
If you attach a handler to the MouseLeftButtonDown event on the ListView it will only fire when areas outside of a ListViewItem are clicked. Any clicks inside the items will be handled by the items themselves to drive the ListView's selection behavior.
You can make changes to the clickable areas by adjusting the Background ({x:Null} is not clickable, anything else is) and Margin of the ListViewItems by setting an ItemContainerStyle on the ListView. Also make sure that you are not using a null Background on the ListView itself (White is the default, Transparent works too).
ListBoxItem control handles clicks on ListBox. You should either:
use PreviewMouseDown event on ListBox
Add event handler in code via myListBox.AddHandler method
See How to Attach to MouseDown Event on ListBox for explanation and code examples.
I found if I had previously single clicked on an item in the listview (and thereby selecting it), then next double clicked on the empty space in the listview the undesired result of having the previously selected item being actioned as if it had been double clicked on (rather then the empty space). To get around this I used the following code (vb.net):
Private Sub ListView1_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles ListView1.MouseLeftButtonDown
ListView1.SelectedIndex = -1
End Sub
with this code in place double clicking on the empty space de-selects any previously selected items and has the desired effect of nothing appearing to happen for the user when they double click in an empty area.

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.

Resources