C# 选择列表框中的项目时打开弹出窗口
我有以下代码在列表框中列出图像。目前我有一个mouseover事件,在IsMouseOver时显示一个弹出窗口。我正试图找出如何将其更改为IsSelected,但在当前上下文中并不存在这种情况。是否有办法更改绑定,以便在选择项目时打开弹出窗口?我曾尝试从代码隐藏(SelectionChanged)中执行此操作,但无法针对弹出窗口C# 选择列表框中的项目时打开弹出窗口,c#,wpf,listbox,C#,Wpf,Listbox,我有以下代码在列表框中列出图像。目前我有一个mouseover事件,在IsMouseOver时显示一个弹出窗口。我正试图找出如何将其更改为IsSelected,但在当前上下文中并不存在这种情况。是否有办法更改绑定,以便在选择项目时打开弹出窗口?我曾尝试从代码隐藏(SelectionChanged)中执行此操作,但无法针对弹出窗口 <ListBox ItemContainerStyle="{StaticResource Fisk}" x:Name="listy" ItemsSource="{
<ListBox ItemContainerStyle="{StaticResource Fisk}" x:Name="listy" ItemsSource="{Binding Images}" Margin="40,0,0,0" Grid.Row="1" Grid.Column="1" Background="{x:Null}" BorderBrush="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" KeyDown="listy_KeyDown" Loaded="listy_Loaded" SelectionChanged="listy_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<vwp:VirtualizingWrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="item">
<Image Width="250" ToolTipService.Placement="Center">
<Image.Source>
<BitmapImage UriSource="{Binding Path=., Mode=OneWay,UpdateSourceTrigger=Explicit}" CreateOptions="DelayCreation" CacheOption="None" />
</Image.Source>
</Image>
<Popup IsOpen="{Binding ElementName=item,Path=IsMouseOver, Mode=OneWay}" Placement="Center">
<Border BorderBrush="Black" BorderThickness="4">
<Image Width="280" ToolTipService.Placement="Center">
<Image.Source>
<BitmapImage UriSource="{Binding Path=., Mode=OneWay,UpdateSourceTrigger=Explicit}" CreateOptions="DelayCreation" CacheOption="None" />
</Image.Source>
</Image>
</Border>
</Popup>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您应该像这样使用
相对资源
绑定:
<Popup IsOpen="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},
Path=IsSelected, Mode=OneWay}"
Placement="Center">
<!-- ... -->
</Popup>
这在可见视图中似乎有效,但如果向下滚动并加载更多图像,则会出现以下异常:mscorlib.dll中出现“System.InvalidOperationException”类型的未处理异常其他信息:集合已修改;枚举操作可能无法执行。@NeoID您刚才提到的错误与答案无关。似乎您可能需要研究vwp:virtualzingwrappanel