列表框IsSelected中的项目时打开弹出窗口

时间:2014-09-20 08:31:43

标签: c# wpf listbox

我有以下代码列出列表框中的图像。目前我有一个鼠标悬停事件,在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>

1 个答案:

答案 0 :(得分:1)

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

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