【问题标题】:Open popup when item in listbox IsSelected当列表框中的项目 IsSelected 时打开弹出窗口
【发布时间】:2014-09-20 08:31:42
【问题描述】:

我有以下代码在列表框中列出图像。目前我有一个鼠标悬停事件,它在 IsMouseOver 上显示一个弹出窗口。我试图弄清楚如何将其更改为 IsSelected,但这在当前上下文中不存在。有没有办法更改绑定,以便在选择项目时打开弹出窗口?我尝试从代码隐藏 (SelectionChanged) 执行此操作,但我无法定位弹出窗口。

<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>

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:

    您应该像这样使用RelativeSource 绑定:

    <Popup IsOpen="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},
                                            Path=IsSelected, Mode=OneWay}"
           Placement="Center">
        <!-- ... -->
    </Popup>
    

    【讨论】:

    • 这似乎在可见视图中有效,但如果我向下滚动并且它应该加载更多图像,我会收到以下异常:mscorlib 中发生了“System.InvalidOperationException”类型的未处理异常。 dll 附加信息:集合已修改;枚举操作可能无法执行。
    • @NeoID 您刚才提到的错误与答案无关。似乎您可能需要研究 vwp:VirtualizingWrapPanel 的实现
    猜你喜欢
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多