如何在ListView中禁用默认选择?

时间:2012-10-03 20:26:22

标签: c# listview windows-8 microsoft-metro

在详细信息页面上,我有一个列表视图和一个控件,根据列表视图中的选定项目显示详细信息。在主页面上,显示了此集合中的几个项目(再次显示在详细信息页面中)。如果用户单击其中一个,则会导航到详细信息页面并显示详细信息。不幸的是,每次用户点击主页面上的项目时,选择更改事件都会被提升两次:一次是第一项(默认项目),一次是所选项目。我怎样才能正确处理这个? - 我只需要第二次(真实)活动。

我找到了post,但无法解决我的问题...

这是我的代码(缩短):

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

}

谢谢你的帮助!

3 个答案:

答案 0 :(得分:2)

从XAML代码中删除SelectionChanged处理程序。在你的代码隐藏中,将SelectedIndex设置为-1(或默认项的索引,如果有的话),然后在设置SelectedIndex后添加处理程序。

编辑:只是为了提供一些背景知识,如果以编程方式更改列表视图的所选项目(例如,当您为SelectedIndex或SelectedItem属性分配新值时),也会触发SelectionChanged事件。我不太确定当你更改ItemsSource时它是否会触发,但我认为情况就是如此。因此,您希望在完成初始化后添加事件处理程序。

答案 1 :(得分:1)

我试验了同样的问题。 我发现“SelectionChanged”事件已经注册了两次。 一次在Page Constructor上,另一次在* .g.cs文件上,那就是“设计者”文件。

我解决了它在de class的构造函数中删除selecionchanged注册。

我希望它可以帮助你。

答案 2 :(得分:-1)

我认为你需要决定在SelectionChanged事件中做出什么回应(也许你想要取消选择被选中的东西?我不确定你要做什么)。也许this post可能会有所帮助。

相关问题