3

On a detail page, I have a listview and a control that shows the details according to the selected item in the listview. On the main page, there are several items shown of this collection (which are again shown in the detail page). If the user clicks one of them, it navigates to the detail page and shows the details. Unfortunately, every time a user clicks an item on the main page, the selection-changed event gets raised two times: Once for the first item (default item) and one for the selected item. How can I correctly handle this? - I only need the second (real) event.

I found this post, but couldn't solve my problem...

This is my code (shortened):

MainPage.xaml:

<GridView
    x:Name="itemGridView"
    ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
    ItemTemplate="{StaticResource Custom250x250ItemTemplate}"
    SelectionMode="None"
    IsItemClickEnabled="True"
    ItemClick="ItemView_ItemClick">

    <!-- details -->

 </GridView>

MainPage.xaml.cs:

 void ItemView_ItemClick(object sender, ItemClickEventArgs e)
 {
    // Navigate to the appropriate destination page, configuring the new page
    // by passing required information as a navigation parameter
    var itemId = ((ArticleDataItem)e.ClickedItem).Id;
    this.Frame.Navigate(typeof(ItemDetailPage), itemId);
 }

ItemDetailPage.xaml:

 <ListView
    x:Name="itemListView"
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
    IsSwipeEnabled="False"
    SelectionChanged="ItemListView_SelectionChanged"
    ItemTemplate="{StaticResource Standard130ItemTemplate}" 
    ItemContainerStyle="{StaticResource CustomListViewItemStyle}" 
    />

 <!-- ... -->

 <WebView x:Name="contentBrowser" />

ItemDetailPage.xaml.cs:

 void ItemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
    // this event gets raised two times when navigating from the MainPage to this page !!!

    // Invalidate the view state when logical page navigation is in effect, as a change
    // in selection may cause a corresponding change in the current logical page.  When
    // an item is selected this has the effect of changing from displaying the item list
    // to showing the selected item's details.  When the selection is cleared this has the
    // opposite effect.
    if (this.UsingLogicalPageNavigation()) this.InvalidateVisualState();

    if (itemsViewSource.View.CurrentItem == null)
    {
         return;
    }

    // reset contentBrowser-Content 
    contentBrowser.NavigateToString(Tasks.WebViewPreCode + "<p>Loading...</p>" + Tasks.WebViewAfterCode);

    var selectedItem = (ArticleDataItem)this.itemsViewSource.View.CurrentItem;

    if ( selectedItem == null )
    {
         contentBrowser.NavigateToString(Tasks.WebViewPreCode + "<p>There was an error. Please try again later.</p>" + Tasks.WebViewAfterCode);
        return;
    }

    // Download item details
    ArticleDataSource.ReadArticle(selectedItem); // this really shouldn't be called two times

}

thank you for your help!

Community
  • 1
  • 1
casaout
  • 1,819
  • 3
  • 24
  • 54

3 Answers3

1

Remove the SelectionChanged handler from your XAML code. In your code-behind, set SelectedIndex to -1 (or the index of the default item, if any), then add the handler after you've set the SelectedIndex.

EDIT: Just to provide some background, the SelectionChanged event is also fired if the list view's selected item is changed programmatically (e.g. when you assign a new value to the SelectedIndex or SelectedItem property). I'm not quite sure if it fires when you change the ItemsSource as well, but I would assume that would be the case. Hence, you want to add the event handler after you've finished up with initialization.

Akinwale
  • 1,055
  • 9
  • 12
  • hi Akinwale Thank you for your answer. Unfortunately, that didn't help me. As you described, I removed the SelectionChanged handler from the XAML code and added it in the code behind (constructor) after I set the selectedIndex to -1. Have I possibly done something wrong or is there another way to solve my problem? - Thanks! – casaout Oct 07 '12 at 17:52
  • Unfortunately, I couldn't solve my problem but have to choose an answer. This was the best suggestion of the possible answers... – casaout Nov 05 '12 at 14:53
1

I experimented the same problem. I found that the "SelectionChanged" event was registered twice. One time on the Page Constructor, and another time on the *.g.cs file, thats looks that a "designer" file.

I solved it removing selecionchanged registration on the constructor of de class.

I hope that it can help you.

-1

I think you need to decide what to respond to in the SelectionChanged event (Maybe you want to unselect something that was selected? I'm not sure what you're trying to do). Perhaps this post may help.

Community
  • 1
  • 1
sfogle
  • 386
  • 3
  • 7
  • I tried to improve the description of my problem... thanks for your help! – casaout Oct 04 '12 at 05:55
  • Ok. I'm still a little confused, but have you thought about the design of your application? Why have you chosen not to use [MVVM](http://msdn.microsoft.com/en-us/magazine/dd419663.aspx) and the [Command interface](http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx)? It is possible that you can solve your problem by getting away from responding to events and designing your app better. – sfogle Oct 04 '12 at 13:20
  • I am using MVVM - I think (and the link I added in my first post supports that), that this has something to do with the ListView... – casaout Oct 04 '12 at 14:26
  • If you were using MVVM, I would try binding the SelectedItem of the ListView to a property in the ViewModel. Depending on what this property is set to, the ViewModel would offer up the proper details page for your detail page xaml to bind to. You can also handle Navigation in the ViewModel though simply using the INotifyPropertyChanged interface could suffice. It seems to me you're either not using MVVM or not taking full advantage of it. – sfogle Oct 04 '12 at 16:20
  • As you may have seen, I have a WebView-control to show the details. Unfortunately you cannot make Databinding to this control. Does anyone have any ideas how I can omit getting two events? - thanks. – casaout Oct 04 '12 at 20:20