As you click on each item in a ListView? - c#

I have created a ListView, but don't know where to start to make clickable each element of the list.
My purpose would be to display in a different hubSection a lyrics of a song, based on the chosen one.
I created the:
SelectionChanged="TestiCanzone_SelectionChanged"
I have a method with this name. should I do there?

No, use ItemClicked to know when the item is clicked. SelectionChanged event is fired when the items are selected or not.
Then in your handler, access ClickedItem property of ItemClickEventArgs arguments.

Related

Trigger click event on specific item in ListView

I've a ListView, each item of the list have associated an event with: MouseLeftButtonDown, this event called a method that will display some details of the item clicked.
Now what I'm trying to achieve is trigger this event by selecting the item behind code. Suppose that in listview I've this item:
Foo
Foo2
I've Foo2 stored in an object variable. What I did:
object currentItem = "Foo2";
MyList.SelectedItem = currentItem;
the problem's that isn't possible fire the MouseLeftButtonDown by selecting an item behind code.
What I need is fire MouseLeftButtonDown on the ListViewItem contained inside the variable currentItem, that of course, is available also in the list MyList.
Is possible do this? Thanks.
Maybe you should use listView_SelectionChanged event to manuplate your data.
Does the selected item contain all the information you want to display? If so, you could simply bind the elements displaying additional information directly to properties of the ListBox.SelectedItem. If you don't want the elements to display until there is an item selected, bind the visibility of the panel containing the elements to the ListBox's SelectedItem property with a NullToVisibilityConverter (mentioned several times on StackOverflow).
This way you have a completely declarative view on your UI, drastically reducing the complexity of the code behind.

C#: bizzare multiple event firing for listview on SelectedIndexChanged

If I put a listview component into a windows form and add bellow code to it's SelectedIndexChanged event:
MessageBox.Show("Fired!");
foreach (int selectedIndex in listView1.SelectedIndices)
{
listView1.Items[selectedIndex].Selected = false;
listView1.Items[selectedIndex].Focused = false;
}
the message box will be shown 4 times! why is that?
Note: I use the loop to clear the selected items in listview
You should not change the selection in a SelectedIndexChanged event. More generally, you should not change a property inside of a notification that the property has been changed.
If you need to change a property in response to a notification, look to handle the corresponding *Changing event. Rather than being a notification that something has changed (which comes after the fact), it is a notification that something is about to change (which comes before the fact). In the SelectedIndexChanging event, you have a couple of different options to alter the course of events:
You can set the e.Cancel property to true, which will do just as it says. It will cancel the event and prevent the selected index from changing.
You can use the e.NewSelectedIndex property to alter the selection. Just set this property to the index of the item that you want to be selected.
And if you want to clear the selected items in a ListView in response to some other event (e.g., a click on a "Clear Selection" button that is not part of the ListView, or a similar context menu item), you don't need a loop at all. Just clear the control's SelectedItems collection: myListView.SelectedItems.Clear(). Again, you can't do this in response the SelectedIndexChanged event, or you'll have the same problem of triggering a bunch of notifications.
Honestly, though, the code you've written here makes no sense. Why would you want to clear all selected items when the user tries to select an item? If you don't want to allow selection, disable the control by setting its Enabled property to false.

silverlight listbox selection area

Am having a list box item template, using wrap panel in container to show as three column listbox. working well. now the issue is, i have to capture the selection change only when user clicks on the second column.
is it possible to set the selection area in listbox?
I don't think it is possible to somehow raise selection change event only when the second or third column is selected. One thing you can do is keep last selected column index in a variable and inside your selection change event judge whether the new selected item belongs to same column or not if it doesn't simply ignore the change
I agree with Haris, I don't think it it possible. You should be able to use the mouse down event and then in code figure out the selection index from that. You could possibly use LINQ on the list box's ItemSource and find the match or use the Tag property of the item.

How to know which generated button was pushed in listbox?

I'm developing a WP7 app and have a listbox with generated buttons all supposed to lead to somewhere specific. I can't figure out how to know which button was pushed at runtime. The list gets generated from a collection of objects with a couple of attributes in each. One of those attributes contain a value that I need to get to be able to know where to send the user.
So my desired process is that the user clicks on an item in the listbox, passing the value of the attribute in the object the button was generated from, to a click handler which sends the user to the right place.
Any suggestions?
I presume your ListBox contains a ItemTemplate which constructs a Button for each of the items bound to your list? if this is the case, within your Click event handler you need to inspect the DataContext of the button that was clicked:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
var myObject = btn.DataContext;
}
As an aside, if you are using this for navigation, a ListBox will not give you very good performance. See the following blog post for an alternative:
http://www.scottlogic.co.uk/blog/colin/2011/04/a-fast-loading-windows-phone-7-navigationlist-control/
Check the sender property of the OnClick event handler for the click handling.
Alternatively you may want to handle the SelectionChanged event of the ListBox and then query the contents of the SelectedItem.

WPF Listview/Gridview setting selected item when combobox is clicked

I'm having issues using a GridView as a ListView's View, what I want to do is fire an event when a user makes a selection from a combobox within the Gridview and pass the selected item within the event.
My first issue is that when the user clicks the combobox within a row, the row isnt selected (meaning the selecteditem stays null unless they click elsewhere first). Is there a clean way to do this without trying to catch mouse clicks or anything?
Secondly theres no selectionchangecommited event on a WPF combobox, is there a cleaner way to check if a user has manually selected an option other than checking if the combobox is enabled?
Thanks
I am seeing a similar behavior. My hypothesis is that one or more layers of the DataTemplate of each item in the list is swallowing the RoutedEvent that should have resulted in a new selection. Is it possible to, in a generic way, tell the items in the DataTemplate that they should never stop the event from bubbeling or tunneling further, without having to override every focus-triggering event handler in code behind?
I 'solved' my issue by using the WPF Toolkit grid (http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117) and a series of selected items, which is probably a cleaner solution anyway.
Doesn't explain the behavior of the GridView, which is to me unusual
Original link has died, believe this to be the one https://github.com/xceedsoftware/wpftoolkit/wiki/DataGrid

Categories