C# 如何在ListView中禁用默认选择?
在详细信息页面上,我有一个listview和一个控件,该控件根据listview中的选定项显示详细信息。在主页面上,显示了此集合的几个项目(在详细信息页面中再次显示)。如果用户单击其中一个,它将导航到详细信息页面并显示详细信息。不幸的是,每次用户单击主页上的项目时,selection changed事件都会引发两次:第一个项目(默认项目)一次,选定项目一次。我怎样才能正确处理这个问题我只需要第二个(真实的)事件 我找到了这个,但解决不了我的问题 这是我的代码(简称): MainPage.xaml:C# 如何在ListView中禁用默认选择?,c#,listview,windows-8,microsoft-metro,C#,Listview,Windows 8,Microsoft Metro,在详细信息页面上,我有一个listview和一个控件,该控件根据listview中的选定项显示详细信息。在主页面上,显示了此集合的几个项目(在详细信息页面中再次显示)。如果用户单击其中一个,它将导航到详细信息页面并显示详细信息。不幸的是,每次用户单击主页上的项目时,selection changed事件都会引发两次:第一个项目(默认项目)一次,选定项目一次。我怎样才能正确处理这个问题我只需要第二个(真实的)事件 我找到了这个,但解决不了我的问题 这是我的代码(简称): MainPage.xaml
<GridView
x:Name="itemGridView"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
ItemTemplate="{StaticResource Custom250x250ItemTemplate}"
SelectionMode="None"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick">
<!-- details -->
</GridView>
<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:
<GridView
x:Name="itemGridView"
ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
ItemTemplate="{StaticResource Custom250x250ItemTemplate}"
SelectionMode="None"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick">
<!-- details -->
</GridView>
<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 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);
}
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
}
void ItemListView\u SelectionChanged(对象发送者,selectionchangedventargs e)
{
//从主页导航到此页面时,此事件将引发两次!!!
//当逻辑页面导航生效时,作为更改使视图状态无效
//in selection可能会导致当前逻辑页发生相应的更改。当
//选择了一个项目,这会改变显示项目列表的效果
//显示所选项目的详细信息。清除选择后,此选项将显示
//相反的效果。
如果(this.usingLogicalAgenavigation())this.InvalidateVisualState();
if(itemsViewSource.View.CurrentItem==null)
{
返回;
}
//重置内容浏览器内容
contentBrowser.NavigateToString(Tasks.WebViewPreCode+“加载…”+Tasks.WebViewAfterCode);
var selectedItem=(ArticleDataItem)this.itemsViewSource.View.CurrentItem;
如果(selectedItem==null)
{
contentBrowser.NavigateToString(Tasks.WebViewPreCode+”出现错误。请稍后重试。“+Tasks.WebViewAfterCode);
返回;
}
//下载项目详情
ArticleDataSource.ReadArticle(selectedItem);//这真的不应该被调用两次
}
谢谢你的帮助 我认为您需要决定在SelectionChanged事件中响应什么(可能您想取消选择已选择的内容?我不确定您想做什么)。可能会有所帮助。从XAML代码中删除SelectionChanged处理程序。在代码隐藏中,将SelectedIndex设置为-1(或默认项的索引,如果有),然后在设置SelectedIndex后添加处理程序
编辑:只是为了提供一些背景信息,如果列表视图的选定项以编程方式更改(例如,当您为SelectedIndex或SelectedItem属性指定新值时),也会触发SelectionChanged事件。我不太确定当您更改ItemsSource时它是否也会触发,但我假设情况就是这样。因此,您希望在完成初始化后添加事件处理程序。我也尝试过同样的问题。 我发现“SelectionChanged”事件注册了两次。 一次在页面构造函数上,另一次在*.g.cs文件上,这看起来是一个“设计器”文件 我解决了在de类的构造函数上删除selecionchanged注册的问题
我希望它能对您有所帮助。您能在发生这种情况的地方显示您当前使用的代码吗?@DJKRAZE谢谢,我添加了我的代码。我试图改进对我的问题的描述。。。谢谢你的帮助!好啊我仍然有点困惑,但是您是否考虑过应用程序的设计?为什么您选择不使用和?您可以通过不响应事件和更好地设计应用程序来解决问题。我使用的是MVVM-我认为(我在第一篇文章中添加的链接支持这一点),这与ListView有关…如果您使用的是MVVM,我将尝试将ListView的SelectedItem绑定到ViewModel中的属性。根据此属性的设置,ViewModel将为您的详细信息页xaml提供适当的详细信息页以绑定到。您还可以在ViewModel中处理导航,尽管只需使用INotifyPropertyChanged接口即可。在我看来,你要么没有使用MVVM,要么没有充分利用它。正如你可能已经看到的,我有一个WebView控件来显示细节。很遗憾,您无法将数据绑定到此控件。有没有人知道我怎么能省去两件事谢谢你的回答。不幸的是,这对我没有帮助。正如您所描述的,在将selectedIndex设置为-1后,我从XAML代码中删除了SelectionChanged处理程序,并将其添加到代码隐藏(构造函数)中。我是否可能做错了什么,或者是否有其他方法来解决我的问题谢谢不幸的是,我不能解决我的问题,但必须选择一个答案。这是可能的答案中最好的建议。。。