Cannot Select List View Item Twice - c#

I am having an issue with selecting list view items twice in a row. So when I select one item it loads another list. This is not a problem but when I click back to go back to the previous list view I am no longer able to click the same list view item.
I have done some reading and there seems to be an idea of 'de-selecting' the list view item at some point in the code so being able to select the same item again.
The selection is done using the MVVM model so the code that handles the selection etc.
// bound to list items on front end, reacts to tap on each item
// and loads route information for the route that is selected
RouteInfo _selected_item;
public RouteInfo RouteLabelSelected
{
get { return _selected_item; }
set
{
if (Equals(value, _selected_item)) return;
_selected_item = value;
OnPropertyChanged(nameof(RouteLabelSelected));
OpenRoutePage(_selected_item.ID);
}
}
The OpenRoutePage method simply opens the next list view, as I said this works fine.
I have attached some images to better illustrate the problem.
Any help would be appreciated, forgive the artwork.

in your ItemSelected event handler, you need to set SelectedItem = null
protected void ItemSelected(object source, ItemSelectedEventArgs args) {
// do whatever actions on selected item here
// then reset SelectedItem
((ListView)source).SelectedItem = null;
}

You can create to different event handlers to solve the issue, namely ItemTapped and ItemSelected event handlers. The ItemSelected event is triggered first on clicked/tapped and then it triggers the ItemTapped event. On first tap/click on a listview item, the ItemTapped event is triggered twice after triggering the ItemSelected event. To overcome this issue, you can attach both event handlers and simply set/clear a boolean property.
Example:
private bool _isSelected;
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
_isSelected = true;
}
private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
if(!_isSelected)
{
//do work here
}
_isSelected = false;
}
Here, first the ItemSelected event is triggered and the variable _isSelected is set. Then the ItemTapped event is triggered twice. The condition in the ItemTapped event prevents the user code from executing in the first call and then allows the code execution in the second call.

Related

Setting selected value does not trigger SelectedIndexChanged

I have a situation where I need the SelectedIndexChanged to fire when I programatically select an item. Here is what I am doing.
I load a dropdownlist from the database on FormLoad and I am setting the value of the drop down based on information passed in the query string like this:
if (Request.QueryString["nat"] != null)
{
ddlTriggers.ClearSelection();
ddlTriggers.Items.FindByValue(
Request.QueryString["nat"].ToString()).Selected = true;
}
This does work correctly in that it takes the id from the querystring and matches it up to a particular item in the drop down. The issue is that just by setting .selected=true does not fire the selectedIndexChanged event. I am trying to set some labels when the selected index is changed.
Some suggestions showed manually calling selectedIndexChanged like this:
ddlCommonTasks_SelectedIndexChanged(ddlTriggers, EventArgs.Empty);
but then that resets the selectedIndex and shows me the label for the 1st item in the drop down which of course doesnt help me any.
Any suggestions.
The problem is that you are changing the selected index on post back or on page load at server. The event will not fire. What you can do is to take stuff in SelectedIndexChanged handler and refactor it into separate method which can be called whenever you update value like:
ddlCommonTasks_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = sender as DropDownList;
PerformIndexChangedAction(ddl);
}
private void PerformIndexChangedAction(DropDownList ddl)
{
lblTest.Text=ddl.SelectedItem.Text;
}
Modify the code as follows:
var nat = Request.QueryString["nat"];
if (!String.IsNullOrWhitespace(nat))
{
ddlTriggers.ClearSelection();
ddlTriggers.Items.FindByValue(nat).Selected = true;
PerformIndexChangedAction(ddlTriggers);
}
If I'm right in assuming that you have registered a handler for the selectedIndexChanged event, you could simply call the same function that you registered as a handler for your event.
If you've registered your handler like this:
$( ".target" ).change(function() {
alert( "Handler for .change() called." );
});
You could instead define your function elsewhere, and pass that:
function handler() {
alert( "Handler for .change() called." );
}
$(".target").change(handler);
Now, in your existing code just call your handler function:
if (Request.QueryString["nat"] != null)
{
ddlTriggers.ClearSelection();
ddlTriggers.Items.FindByValue(Request.QueryString["nat"].ToString()).Selected = true;
handler();
}

How to access the Index of a changed Selection in a Datagridview

I am working on a windows form. Currently I am geting quite a headache over a seemingly simple prolbem.
I have a datagridview and want to allow right click selection. Therefore I created a function called dataGridViewJobs_MouseDown that is raised on MouseDown on the datagridview:
private void dataGridViewJobs_MouseDown(object sender, MouseEventArgs e)
{
int curRowIdx = dataGridViewJobs.HitTest(e.Location.X, e.Location.Y).RowIndex;
if (curRowIdx != -1 && curRowIdx < dataGridViewJobs.Rows.Count)
{
dataGridViewJobs.CurrentCell = dataGridViewJobs[0, curRowIdx];
}
}
A hitTest is executed to find the row-index of the clicked cell. Afterwards the currentCell of the datagridview is set to the first cell in said row.
This causes the SelectionChanged-event to be raised. This is connected to the following function:
private void dataGridViewJobs_SelectionChanged(object sender, EventArgs e)
{
if (dataGridViewJobs.Rows.Count > 0)
{
Console.WriteLine(dataGridViewJobs.CurrentCell.RowIndex);
}
}
This writes the old index to the console. Why is that?
I am currently working with a workaround, which means I save the result of the hitTest in a global variable. But that can not be the right way to do this.
Am I doing something wrong? Thanks in advance!
From MSDN
When you change the value of the CurrentCell property, the
SelectionChanged event occurs before the CurrentCellChanged event. Any
SelectionChanged event handler accessing the CurrentCell property at
this time will get its previous value.
Use CurrentCellChanged event for printing current value

Item realized event does not fire

Quick question about the Item realization event in WP8.
Here is my event registration which I call in the ctor for the View.
EpisodeList.ItemRealized += EpisodeList_ItemRealized;
Also here is my EventHandler
private void EpisodeList_ItemRealized(object sender, ItemRealizationEventArgs e)
{
if (!vm.Loading && EpisodeList.ItemsSource != null && EpisodeList.ItemsSource.Count >= _offset)
{
if (e.ItemKind == LongListSelectorItemKind.Item)
{
if ((e.Container.Content as Medium).Equals(EpisodeList.ItemsSource[EpisodeList.ItemsSource.Count - _offset]))
{
//Ask Messenger to notify the ViewModel To Load More Items
Messenger.Default.Send<MainPageLoadMoreEpisodesMessage>(new MainPageLoadMoreEpisodesMessage());
}
}
}
}
My problem is that the event handler fires a few times but then it never fires again, I have no clue why I event tried to register the event handler again after loading was complete, I am unable to get the event handler to fire again.
LongListSelctor Item Realized & Item Unrealized event fires when longlistselector scroll down or top, it take 20 element to realize at once, to fire it again it need more than 20 item in list. It don't need to register twice.

c# how do i get check if a selected item in a listbox has changed. ?

i understand listbox.selectedItem will get me the current item thats seletected in a listbox... how do i get if the current item has been changed. so i want to know if the user's selection has changed from what it was to a different item.
Subscribe to the SelectedIndexChanged event like this:
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
The listener is:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Add code in the listener above to react to the event.
You can listen for SelectedIndexChanged event, this is Windows.Forms, naturally.

In C# WPF, why is my TabControl's SelectionChanged event firing too often?

I have a tabbed GUI with each tab containing a Frame. In one of these Frames there is a DataGrid. When the user selects this tab, I need my datagrid sorted, so I'm using the TabControl SelectionChanged event to trigger the sort. However, this event triggers every time an item is selected from the DataGrid, even though the tabs themselves remain untouched.
I've tried number of different events:
GotFocus for a TabItem
RequestBringIntoView for a TabItem
but they all seem to suffer from this problem. What is causing this?
The TabControl.SelectionChanged is the same event as a ComboBox.SelectionChanged
It originates from Selector.SelectionChanged.
So, if you do not mark your event as handled in your event handler, it will bubble up the tree, and eventually arrive at your TabControl, which is causing this "firing too often" issue.
Mark your event as handled in your SelectionChanged of your ComboBox/ListBox/ListView/any other Selector you use in your DataGrid like so:
private void MyComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
e.Handled = true;
}
And this inconvenience will go away ;).
private void tabControlName_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.Source is TabControl) //if this event fired from TabControl then enter
{
if (tabItemName.IsSelected)
{
//Do your job here
}
}
}
If you have added a handler with AddHandler in a parent element, all selection changes will fire the SelectionChanged-event. In this case, you can give your TabControl a name and then check in the EventHandler if the name of the OriginalSource is the name of your TabControl.
Another good approch is adding a handler to the tabControl.Items.SelectionChanged:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ItemCollection view = tabControl.Items;
view.CurrentChanged += new EventHandler(view_CurrentChanged);
}
void view_CurrentChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
Maybe is not the xamly way, but is less pain as it only fires when an item is changed.

Categories

Resources