【问题标题】:How to disable default selection in a ListView?如何禁用 ListView 中的默认选择?
【发布时间】:2012-10-03 20:26:21
【问题描述】:

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

我找到了这个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

}

感谢您的帮助!

【问题讨论】:

  • 您能在发生这种情况的地方显示您当前正在使用的代码吗?
  • @DJKRAZE 谢谢,我添加了我的代码。

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


【解决方案1】:

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

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

【讨论】:

  • 嗨 Akinwale 谢谢您的回答。不幸的是,这对我没有帮助。如您所述,在将 selectedIndex 设置为 -1 后,我从 XAML 代码中删除了 SelectionChanged 处理程序并将其添加到后面的代码(构造函数)中。我可能做错了什么还是有其他方法可以解决我的问题? - 谢谢!
  • 很遗憾,我无法解决我的问题,但必须选择一个答案。这是可能答案的最佳建议......
【解决方案2】:

我尝试了同样的问题。 我发现“SelectionChanged”事件被注册了两次。 一次是在页面构造器上,另一次是在 *.g.cs 文件上,这看起来像是一个“设计器”文件。

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

希望对你有帮助。

【讨论】:

    【解决方案3】:

    我认为您需要决定在 SelectionChanged 事件中响应什么(也许您想取消选择已选择的内容?我不确定您要做什么)。也许this post 可能会有所帮助。

    【讨论】:

    • 我试图改进对我的问题的描述...感谢您的帮助!
    • 好的。我还是有点困惑,但是你有没有想过你的应用程序的设计?为什么选择不使用MVVMCommand interface?您可以通过摆脱对事件的响应和更好地设计您的应用程序来解决您的问题。
    • 我正在使用 MVVM - 我认为(我在第一篇文章中添加的链接支持这一点),这与 ListView...
    • 如果您使用的是 MVVM,我会尝试将 ListView 的 SelectedItem 绑定到 ViewModel 中的属性。根据此属性的设置,ViewModel 将为您的详细信息页面 xaml 提供正确的详细信息页面以绑定到。您也可以在 ViewModel 中处理 Navigation,尽管只需使用 INotifyPropertyChanged 接口就足够了。在我看来,您要么没有使用 MVVM,要么没有充分利用它。
    • 您可能已经看到,我有一个 WebView 控件来显示详细信息。不幸的是,您不能对这个控件进行数据绑定。有谁知道如何省略两个事件? - 谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多