I want to manage the listview while the text field is in focus. I mean the following case: I'm writing some text into the text box, but I am able to manage the list view selection in the same time by some keys(Up, Down).
I tried to change the SelectedIndex property, but I lose the automatic scrolling of the list view in this case.
I think it could be solved by raising key down event with a special key (Up, Down) for the listview - is there any way to do it?
you just need to use ScrollIntoView() method of ListView whenever you set the SelectedIndex of your ListView just follow it up with ScrollIntoView. you do it in 2 ways.
subscribe to SelectionChanged event and scroll the new selected item into view.
instead of setting SelectedIndex you can set SelectedItem and then scroll that item into view.
After setting the SelectedIndex, just get the item of that index and then scroll that item into view.
Related
For each item in my listbox, I have a Button and a TextBlock. I can set my button to raise an event that removes an item from the ListBox, if I click on the TextBlock to highlight the item and then Remove(myListBox.SelectedItem).
However, I'm looking for a way to identify which item in the ListBox a specific Remove-Button is attached to, so I can RemoveAt(?) when the event is raised.
Edit: Each of the buttons should remove the item on the line of that button without highlighting the line first. Therefore, using ListBox.SelectedItem is not a viable option :)
You can get the index of the item using the IndexOf method:
YourCollection.RemoveAt(YourCollection.IndexOf(myListBox.SelectedItem));
I have a listview with a button as part of the datatemplate. When I click the button I want an action to happen, but I don't necessarily want that item selected. Is there any way I can stop the tap event from bubbling up to the listbox?
Thanks!
Not really sure this is the best solution but. Capture the selected index before you get to the method the button activates. Inside of it at the end set selected back to the original.
I created a sample ListView with a button in it. Clicking on the button does not select the item for me. Can you post your DataTemplate?
But, you can add the following to your click method:
myListBox.SelectedItem = null;
Where myListBox is the name of your ListBox. This will cause the listbox to have no selected items. If you want to keep the previously selected item, you have to keep the track of previously selected item or index and set the listbox to that item using either:
myListBox.SelectedItem = previousItem;
myListBox.SelectedIndex = previousIndex;
Is there a way to disable the ability to delect an item in ListView?
I have a SplitPage and when I deselect the selected item in ListView on the left the content on the right disappears because of lost databindings. I do not want that. I tried setting the ListView selectedItem back to the last one selected but the the back button in snap mode does not work
You can set the selection mode to none and set your ItemTemplate to a restyled ToggleButton. Then you can bind the Enabled property of each button to the item view model and control the enabledness of each of them buttons separately.
I have a listbox in WPF that's bound to a ObservableCollection using the ItemSource property. This works fine, the correct items are shown.
The listbox (which contains a list of image names) has an event handler on the SelectionChanged event which updates the source of an Image control with the path to the selected image (effectively giving an image preview).
I have the following code to remove an item from the lisbox, on a button's click event:
if (lstLocal.SelectedIndex > -1)
{
localImages.RemoveAt(lstLocal.SelectedIndex);
}
localImages being the ObservableCollection and lstLocal being the ListBox.
However, when I remove the selected item this causes the SelectionChanged event to fire. My SelectionChanged event handler makes use of the SelectedIndex property on the listbox. I get the exeption Index was out of range. Must be non-negative and less than the size of the collection. , so I'm guessing that removing an item causes SelectedIndex to be set to something like null (or a negative number)?
Is there a way around this problem? I'm guessing there's a better way for me to remove items, or I need some sort of check on my SelectionChanged handler?
Wrap the code in your SelectionChanged handler in a
if (lstLocal.SelectedItem != null)
{
...
}
Ah, a classic annoyance of the ListBox control. When altering the bound collection, it can clear the selection and reset the scroll position to the top. It's truly irritating when you have to select items from a list and edit them piecemeal as part of a workflow.
You may want to maintain a "currently selected item" object reference when changing the list so that you can keep the selection if something changes behind the scenes. Then you can also make sure that if it exists, that it stays in view by scrolling back to that item using the ScrollIntoView method.
A SelectedIndex of -1 means nothing is selected, as you have already remembered. This is an ancient hold-over from pre-.NET, before the SelectedItem property existed. Nowadays SelectedItem will be null at the same time as SelectedIndex is -1.
I am having trouble setting the Selected Item of a Listbox I am populating and adding to the LayoutRoot's children in code.
I am creating the ListBox over when going back to the page, so I am saving a variable which will tell me what the selected item was before the user clicked.
I tried setting SelectedIndex, but that did not seem to work. That selects the item and calls SelectionChanged, but the item does not come into focus.
I also tried the combination of MyListBox.ScrollIntoView(MyListBox.Items[MyListBox.SelectedIndex]) and MyListBox.UpdateLayout(), but that did not seem to work either. The item does not seem to come into focus.
Try MyListBox.SelectedItem.EnsureVisible().
(If it doesn't have SelectedItem (ListBox on the desktop CLR doesn't), then use SelectedItems[0] instead)
E: Okay, looks like Windows Phone doesn't support that. However, it does support MyListBox.EnsureVisible(MyListBox.SelectedItems[0])
I ended up fixing this by Adding the UserControl that had the ListBox into the Page's XAML, and the ListBox into the UserControl's XAML.
Then, I was able to use the ScrollIntoView(MyListBox.Items[mySavedSelectedItem]);
I simply saved this value when the user made the selection.
mySavedSelectedItem = ((ListBox)sender).SelectedIndex;