Item realized event does not fire - c#

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.

Related

User checked item in CheckedListBox

I have a CheckedListBox. I want to know when the user checked or unchecked an item. I tried using ItemCheck event but it fires even when an item is programmatically checked. How can I detect this?
Using the ItemCheck event handler is the correct method for detecting when the user ticks or un-ticks an item in the CheckedListBox. And yes, it will also fire when the item is checked/unchecked programmitically.
If you don't want the event fired when you set/unset items programmatically, you should remove the event handler before hand.
Assuming your event handler looks like this:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
Debug.Print("Checked");
}
else if (e.NewValue == CheckState.Unchecked)
{
Debug.Print("Un-Checked");
}
}
Before you set/unset items programmatically, you should add the line:
this.checkedListBox1.ItemCheck -= this.checkedListBox1_ItemCheck;
and after the items have been set/unset you in code, re-add the event handler with:
this.checkedListBox1.ItemCheck += this.checkedListBox1_ItemCheck;

Cannot Select List View Item Twice

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.

Using sender to determine what button is pressed in ListView

I have a ListView that has Update and Cancel buttons. Both of these buttons have a CommandName of Cancel, so they fire the same ListView event handler (ListView_ItemCanceling).
Inside this event handle I execute my stored procedures. The issue I am having is since both buttons fire the same event handler they both update. Even if there are no changes being made.
I would like to try to determine the button that has fired the event at the start of the event handler (possibly using sender?), but I cannot figure out how to do this.
This is what I was currently trying to do in the ListView_ItemCancelling event handler:
Button newButton = (Button)sender;
if(newButton.Text == "Cancel")
{
Console.Write("this worked");
}
When I execute this code I get an error message telling me that I cannot convert the sender object from ListView object to a Button object.
Any help will be greatly appreciated!
You can define to command names for each button to detect the which one is click for example:
define the first as "Cancel1" and the other "Cancel2"
and in the code you can check like that:
if(CommandName == "Cancel1")
{
// do some thing
}
else if(CommandName == "Cancel2")
{
// do other staff
}
or if both at doing the same job but you need to determine the sender
if(CommandName == "Cancel1" || CommandName == "Cancel2")
{
// do some thing common
}
if(CommandName == "Cancel1")
{
// do some thing if button 1 clicked
}
if(CommandName == "Cancel2")
{
// do some thing if button 2 clicked
}
I came to the answer with help from #paqogomez. He suggested I use the ItemCommand event handler for the ListView to get the button that is being clicked for the listview.
Inside the ItemCommand event handler I checked them command argument and used the appropriate code thereafter.
protected void LV_Tickets_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if(e.CommandName == "Update")
{
//code here
}
}
The sender seems to be your ListView, not Button. Try using Button_OnClick event instead of ListView_ItemCancelling.
Or try doing some reseach on ListView_ItemCancelling, such as using ListViewCancelEventArgs e parameter, maybe it can help you in this situation. You can read more about it on MSDN.

DataRow Delete without firing CurrentChanged Event

Well, I have the following problem:
I need to delete a row from a bindingsource without fireing the CurrentChanged event. Deletion is working without any problems. But it raises the CurrentChanged event instantly, which leads to my code matching to the CurrentChanged event is being executed. That leads to a problem.
Is there any way to achieve a similar effect like .Delete() without raising the event?
Deleting a row will always raise the event, if there are any subscribers.
If the event code is under your control, you could set a flag which you check in the BindingSource_CurrentChanged event handler:
private void DeleteRow()
{
this.justDeletedRow = true;
this.bindingSource.DeleteRow(...);
}
protected void BindingSource_CurrentChanged(object sender ...)
{
if (this.justDeletedRow)
{
this.justDeletedRow = false;
return;
}
// Process changes otherwise..
}
If the code isn't under your control - if you're binding to a component, say - then you can unbind the handler while performing the operation:
private void DeleteRow()
{
this.bindingSource.CurrentChanged -= this.component.BindingSource_CurrentChanged;
this.bindingSource.DeleteRow(...);
this.bindingSource.CurrentChanged += this.component.BindingSource_CurrentChanged;
}

Changing the month in MonthCalendar fires DateChanged event twice

Heyo,
I have a standard WinForms MonthCalendar in my application with a handler hooked up to the DateChanged event. Whenever I change the date by day, clicking on a certain date in the little calendar, the event fires once. However, whenever I change the date by month, clicking on the < and > in the control, the event fires twice. I want the event to fire once in all situations.
Any ideas?
EDIT: I debugged and found out that the SelectedItem or Range is the same on the first and second handler call. So I need a way to differentiate between the first and second call while still allowing for proper handling when the event only fires once.
The handler code was requested, here it is, but it has nothing to do with the event firing multiple times:
List<TimestampInfo> displayTimestamps = databaseManger.QueryForTimestamps(DayPicker.SelectionRange);
if (displayTimestamps == null) return;
TimestampsListBox.Items.Clear();
TimestampsListBox.Items.AddRange(displayTimestamps.ToArray());
Somewhat of a hack, but compare the SelectionRange string value with the last DataChanged event. Just run your code if it's different:
private string _LastRange = string.Empty;
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e) {
if (monthCalendar1.SelectionRange.ToString() != _LastRange) {
_LastRange = monthCalendar1.SelectionRange.ToString();
List<TimestampInfo> displayTimestamps = databaseManger.QueryForTimestamps(DayPicker.SelectionRange);
if (displayTimestamps == null) return;
TimestampsListBox.Items.Clear();
TimestampsListBox.Items.AddRange(displayTimestamps.ToArray());
}
}
I couldn't reproduce this until I hooked up the event handler twice.
monthCalendar1.DateChanged += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateChanged);
monthCalendar1.DateChanged += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateChanged);
Is you code munging around with the event handlers?
Try this:
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e) {
Calendar1.SelectedDate = Calendar1.VisibleDate;
// any additional code optional
}

Categories

Resources