WPF Listview SelectionChanged事件 [英] WPF Listview SelectionChanged event
问题描述
我有一个ListView
绑定到ItemsSource
,并且SelectionChanged
事件在加载/数据绑定事件上触发?我认为这是因为选择了默认"项,即索引0.
I have a ListView
binding to an ItemsSource
and the SelectionChanged
event is firing on the load/databound events? I assume that it is because a 'default' items ie index 0 is selected.
如何禁用此功能?
推荐答案
感谢您的答复.
当我在SelectionChanged事件上放置一个断点时,它会在屏幕完全加载之前中断程序.您还将在列表中看到第一行随后被选中".正如您在代码中看到的那样,我没有绑定到SelectedIndexValue.列表的DataContext是ReadonlyCollection
When I put a breakpoint on the SelectionChanged event, it breaks proceedings there before the screen is fully loaded. You will also see that the first row is 'selected' afterwards on the list. I am not binding to a SelectedIndexValue as you can see in the code. The DataContext for the list is a ReadonlyCollection
在您的SelectionChanged事件中,您可以看到我通知其他对象要加载与所选项目相关的数据.我只希望在选择一个而不是设置一个默认值时发生这种情况.我必须要代表类似数据的这些ListView,但是在加载时,都必须选择一个项目.
In my SelectionChanged event as you can see I notify other objects to be loaded with data relating to the selected item. I only want this to happen when one is selected but not a default one to be set. I have to of these ListViews representing similar data but on loaded none must have an item selected.
我注意到在Listview的属性窗口上,默认的Selected索引设置为-1.我什至可以将其设置为List_Loaded事件的代码,但是到那时,第一个SelectionChanged已经发生了.
I have noticed that the default Selected index is set to -1 on the properties window for the Listview. I can even set this is code on the List_Loaded event, but by then the first SelectionChanged has happened already.
<ListView PreviewMouseDown="ActiveCasesView_MouseDown" x:Name="ActiveCasesView"
DataContext="{StaticResource ActiveCasesViewSource}"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource CasesItemTemplate}"
SelectionMode="Single"
SelectionChanged="ActiveCasesView_SelectionChanged"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto" >
</ListView>
private void ActiveCasesView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (mouseClicked)
if (e.AddedItems.Count > 0)
App.Messenger.NotifyColleagues(App.MSG_SELECT_ACTIVE_CASE, ((CaseViewModel)ActiveCasesView.SelectedItem).CaseNumber);
}
我添加了PreviewMouseDown来设置一个指示器,该指示器指示我在SelectionChanged事件中单击了列表视图.这确实有帮助,但我不认为这是最佳解决方案.
I added the PreviewMouseDown to set an indicator that I have clicked on the listview in the SelectionChanged event. This does help but I'm not convinced that its the best solution.
谢谢 佩特鲁斯
这篇关于WPF Listview SelectionChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!