WPF Listbox item information - c#

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));

Related

UWP: Raise Key Down programmatically

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.

Selected ListViewItem is not highlighted on TabPage, after selected TabPage changed forth and back

I am currently facing a strange behavior of the C#.Net ListView when used on a TabControl's TabPage.
Background:
I have said TabControl with three TabPages, the ListView is on the first one.
Scenario:
I select one Item in the ListView and its "Selected" property is set to "true"
I change to another TabPage
I change back to the first TabPage with the ListView containing the selected Item
Result:
The former selected Item in the ListView is not highlighted any longer. Iterating over all Items shows that its "Selected" property is still set to "true".
As soon as I click into the ListView control, I can see the selected Item being highlighted for a short moment, before loosing its highlighting again due to me having clicked somwhere else and therefore deselecting it.
But neither one of Refresh, Invalidate, RedrawItems, Focus or Select on the ListView leads to the selected Item being highlighted again.
Has anyone of you experienced the same issue and found a proper way to solve this?
My current solution is to clear the List of SelectedItems on the ListView as soon as the corresponding TabPage is re-focused, hence deselecting every Item and forcing the user to re-select what he wants to be selected.
But since I also noticed that ListBoxes can savely store their selection and display it properly, I would wish for the same feature on the ListView.
Thanks,
Thomas

WPF: Click partially visible row in listbox will scroll up instead of activating controls in the item

I have checkbox as item in a list box.
If you click a partially visible row in the listbox it will scroll up the list view to make the item fully visible, but not check the checkbox.
Is there any way to work around this?

How to stop a button tap event from bubbling into a listview?

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;

WPF Deleting item from listbox

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.

Categories

Resources