我有两页说Main.xaml
和Details.xaml
。每个页面都有一个ListBox,我将它设置为ViewModel(Same collection)中的一个集合。奇怪的是,当我在Details页面中选择一个项目时,它会调用SelectionChanged
事件处理程序Main
页面和Details
page.Is this a bug?列表框在另一页上调用ListBox的SelectionChanged事件处理程序?
我已通过在OnNavigatedFrom
()方法中取消挂接SelectionChanged
事件处理程序解决了此问题。
编辑
在Main.Xaml我有类似如下:
<ListBox Name="MainDataListBox" Margin="8,113,8,8" ItemsSource="{Binding DataList}" SelectionChanged="MainDataListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Border BorderThickness="3" BorderBrush="#A5FFFFFF" Width="80" Margin="0,20,0,20" Height="60">
<Image Source="{Binding ImageUrl, Mode=OneWay}" VerticalAlignment="Stretch" Margin="0,0,0,0" Width="80" Height="60" Stretch="Fill" />
</Border>
<TextBlock TextWrapping="Wrap" Text="{Binding Title}" FontSize="40" FontWeight="Normal" VerticalAlignment="Center" Margin="30,0,0,0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在Details.xaml我也有相同类型的ListBox:
<ListBox Name="DetailDataListBox" Margin="8,113,8,8" ItemsSource="{Binding DataList}" SelectionChanged="DetailDataListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Border BorderThickness="3" BorderBrush="#A5FFFFFF" Width="80" Margin="0,20,0,20" Height="60">
<Image Source="{Binding ImageUrl, Mode=OneWay}" VerticalAlignment="Stretch" Margin="0,0,0,0" Width="80" Height="60" Stretch="Fill" />
</Border>
<TextBlock TextWrapping="Wrap" Text="{Binding Title}" FontSize="40" FontWeight="Normal" VerticalAlignment="Center" Margin="30,0,0,0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
两者的页面主要细节有单独的EvenHandlers MainDataListBox_SelectionChanged
和DetailDataListBox_SelectionChanged
。
问题是,当我在详细信息页面中选择一个项目MainDataListBox_SelectionChanged
被称为第一个,然后DetailDataListBox_SelectionChanged
。 另外我的viewmodel是App.cs中的静态模型,我将这两个页面的数据上下文设置为相同的viewmodel。
感谢和问候
vaysage
听起来像你挂钩两个列表直到同一事件。如果没有代码,它肯定很难说。 – 2011-03-22 12:48:48
@Matt莱西我把这两个列表挂钩到单独的事件。 – Vaysage 2011-03-23 05:08:18
你可以发布一些代码,演示如何重现这一点。 – 2011-03-23 09:22:27