Trigger click event on specific item in ListView - c#

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.

Related

how to detect datagrid items source changed at the UI in WPF?

I have a datagrid which is bound to an Observable collection in my view model.
The items source property is set to the collection.
Every time I update the collection ( through another button ) , my datagrid view updates and i am able to see the changes .
I want to make sure that atleast one item in the WPF datagrid is selected always .
I tried making Selected Index =0 in the Datagrid XAML , but this works only for initially Loaded datagrid. It doesnt reset the selected index to 0 when the Items source collection is changed . By changed I mean the entire Colelction is reset ( not individual items being added or removed).
Basically I need an event that gets fired from WPF whenever the itemssource collection is reset.
not sure if any code here is helfpul.
You can subscribe to the PropertyChanged event on your view model and look at the event args to see if the property name matches the name of your DataGrid's ItemsSource. You'll also want to make sure that you are firing that event in your view model (you probably are because your datagrid shows the change).
You could do this in the view's code behind, or better yet, in an attached property.
There are two types of events one can subscribe to and because you are using MVVM where the collection is on the VM, here are the strategies...
Individual Changes
On the View in code behind, subscribe to the ObservableCollection's CollectionChanged event. When the collection changes, the event will be fired and you can specify an index on the grid to be selected depending on the type action that occured.
Collection Change
For whole collection changes, also from the View subscribe to the VM's InotifyProperty change event and do the same logic as mentioned to set a specific item on the grid to be selected.

How to set focus on the first item of a ListView?

I am trying to implement a little search module in a WPF application. The results of the search are bound to a ListView and I want to set the focus on the first item of this ListView as soon as it is populated with new results.
The solution of this question - Set Item Focus in ListView WPF - suggests calling ContainerFromIndex(int index) method to get the necessary ListViewItem. Trying this at the very end of my search procedure returns null. So, I deduce that although the source of the ListView has been populated, the view itself isn't updated at this moment.
The next thing I tried is handling a SourceUpdated event of my ListView. It doesn't fire. I added NotifyOnSourceUpdated=True to the binding as was pointed out in the answer of this question - sourceupdated event not firing - but the event still doesn't fire.
I'll just add that the binding itself works. So, what is the right way to set focus on the first item (or on any other item) of a ListView?
EDIT: I'll supply the relevant part of the XAML to make things more clear:
<ListView Name="foundRequestsListView"
IsSynchronizedWithCurrentItem="True"
utils:GridViewSort.AutoSort="True"
ItemsSource="{Binding Path=FoundRequests, NotifyOnSourceUpdated=True}"
SourceUpdated="foundRequestsListView_SourceUpdated" >
<ListView.Resources>...</ListView.Resources>
<ListView.View>
<GridView>...</GridView>
</ListView.View>
</ListView>
FoundRequests property is an ordinary List.
EDIT: Calling UpdateLayout() beforehand solves the problem.
The problem is solved by calling UpdateLayout() method before trying to get a ListViewItem.
One way that you can achieve your requirement is to use the selected item that you say you have to access the associated ListViewItem that you can focus. Try this:
ListViewItem item = YourListView.ItemContainerGenerator.
ContainerFromItem(YourSelectedItem) as ListViewItem;
Once you have the associated ListViewItem, you can focus it simply, like this:
item.Focus();
Bind the SelectedItem to a property in your ViewModel.
SelectedItem="{Binding PropertyNameHere}"
When the selected item is changed on the collection, it will also be updated on the property bound to the selected item.
So at the end of your search procedure, simply set the bound property to the first item in the collection.
if (FoundRequests.Count > 0)
PropertyNameHere = FoundRequests[0];
You do not need to worry about INotifyPropertyChanged for the bound property.

Binding SelectedItems of Listview

how can i bind SelectedItems of a ListView?
My ListView has multipleSelection attribute and I'm using CollectionView for its contents..
I've heard about Attached property and I tried implementing this with the one I found here:
Sync SelectedItems in a muliselect listbox with a collection in ViewModel
I can multiple select the items by clicking rows but I can't use the Shift keyboard for multi-selecting many rows instantly... Also, when I filter my collection and refresh it, my selection are all deselected after the refresh..
How can I make it so that whenever my CollectionView refreshes, the previously selecteditems are still selected after the refresh...?
Can someone also help me how to manipulate logically the selected items through my viewmodel?
May be you should add the IsSelected property to the ListViewItem's view model.
You will have to use your own code to keep the selected items after a refresh. Maybe make a copy of your collection before the refresh and afterwards a simple for to check all the checked items in your current collection.
Change your selectection mode to extended for your listbox for the shift key to work.
As for manipulating logically the selected items, you will have to give a lot more info on what exactly you want done.

How to get the name of the collection an item is bound to?

Apologies for not posting any code for this but all my efforts seem to be going nowhere.
I need to get the name of an observableCollection a textblock is bound to from the mouseDown event handler, so I can then perform some operations on the data, is there any way to do this?
I have set the Tag of the textblock to {binding} so i get the entire object back the textblock is bound to. Other than this im not sure where to go next.
Update:
The reason for this is that I have 2 multiselect treeviews, with a heirarchial data template, each template shares the same treeview but is bound to a different observable collection.
The way my multiselect works is by applying a style to each treeview if the IsSelected value of that item in my collection is true.
Now I am using the Mousedown event handler for the textblock in my datatemplate to get the item I am working on, BUT some items can be in both collections at once. I need to know which item to set the IsSelected value on. Using Binding{tag} I get the Item I need to set on but not the collection it is within.
I'm using Mousedown as the event handler because If you click on one item to select it, then click again it needs to unselect and the treeview event handlers didnt seem to allow this to happen (SelectedItemChanged etc).
As a side note I also need to be able to hide the default selected style of the Treeview as this isnt used and it gets confusing.
You can't determine what collection an item is in, but you can determine what TreeView the user clicked on. Then you can get the collection by knowing the TreeView, which should solve your immediate problem.

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