将弹出窗口 PlacementTarget 绑定到列表框 SelectedItem
Binding popup PlacementTarget to listbox SelectedItem
我有以下 XAML(简体):
<Grid>
<Popup Name="Popup" DataContext="{Binding ElementName=GameWheel, Path=SelectedItem}" PlacementTarget="{Binding SelectedItem}" Placement="Center" IsOpen="True">
<Image Source="{Binding ImagePath}" Width="100" />
</Popup>
<DockPanel Margin="40,0">
<ListBox x:Name="GameWheel" ...>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<c:VirtualizingWrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image x:Name="GamesWheelImage" ... />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</Grid>
虽然 DataContext 是正确的并且在弹出窗口中显示了正确的图像,但位置完全错误。我的意图是让弹出窗口覆盖列表框中 SelectedItem 的中心,但由于某种原因它总是位于 window.
的中心
关于如何在列表框中放置所选项目以使弹出窗口始终覆盖所选项目的中心的任何想法?我最接近的是将图像放在列表框的中央,但我无法让它只覆盖所选项目。
在我看来,您应该为属于名为 GameWheel 的列表框的 ListBoxItems 创建一个特定模板。
只是为了给你一个例子,我创建了一个更简单的列表框版本:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="300" Width="400">
<Window.Resources>
<ControlTemplate x:Key="ListBoxItemWithPopup" TargetType="{x:Type ListBoxItem}">
<Border Name="border" Padding="2">
<Grid>
<Popup Name="popup" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Placement="Center" >
<TextBlock Margin="1" Background="White" Foreground="Black" Text="{TemplateBinding Content}" />
</Popup>
<ContentPresenter />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="popup" Property="IsOpen" Value="True"/>
<Setter TargetName="border" Property="Background" Value="Azure" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<DockPanel Margin="40,0">
<ListBox x:Name="GameWheel">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template" Value="{StaticResource ListBoxItemWithPopup}" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Items>
<sys:String>One</sys:String>
<sys:String>Two</sys:String>
<sys:String>Three</sys:String>
<sys:String>Four</sys:String>
</ListBox.Items>
</ListBox>
</DockPanel>
</Grid>
</Window>
如您所见,Popup
直接包含在 ListBoxItem
模板中,并使用触发器显示。
希望我的例子能帮到你。
我有以下 XAML(简体):
<Grid>
<Popup Name="Popup" DataContext="{Binding ElementName=GameWheel, Path=SelectedItem}" PlacementTarget="{Binding SelectedItem}" Placement="Center" IsOpen="True">
<Image Source="{Binding ImagePath}" Width="100" />
</Popup>
<DockPanel Margin="40,0">
<ListBox x:Name="GameWheel" ...>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<c:VirtualizingWrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image x:Name="GamesWheelImage" ... />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
</Grid>
虽然 DataContext 是正确的并且在弹出窗口中显示了正确的图像,但位置完全错误。我的意图是让弹出窗口覆盖列表框中 SelectedItem 的中心,但由于某种原因它总是位于 window.
的中心关于如何在列表框中放置所选项目以使弹出窗口始终覆盖所选项目的中心的任何想法?我最接近的是将图像放在列表框的中央,但我无法让它只覆盖所选项目。
在我看来,您应该为属于名为 GameWheel 的列表框的 ListBoxItems 创建一个特定模板。 只是为了给你一个例子,我创建了一个更简单的列表框版本:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="300" Width="400">
<Window.Resources>
<ControlTemplate x:Key="ListBoxItemWithPopup" TargetType="{x:Type ListBoxItem}">
<Border Name="border" Padding="2">
<Grid>
<Popup Name="popup" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Placement="Center" >
<TextBlock Margin="1" Background="White" Foreground="Black" Text="{TemplateBinding Content}" />
</Popup>
<ContentPresenter />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="popup" Property="IsOpen" Value="True"/>
<Setter TargetName="border" Property="Background" Value="Azure" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<DockPanel Margin="40,0">
<ListBox x:Name="GameWheel">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template" Value="{StaticResource ListBoxItemWithPopup}" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Items>
<sys:String>One</sys:String>
<sys:String>Two</sys:String>
<sys:String>Three</sys:String>
<sys:String>Four</sys:String>
</ListBox.Items>
</ListBox>
</DockPanel>
</Grid>
</Window>
如您所见,Popup
直接包含在 ListBoxItem
模板中,并使用触发器显示。
希望我的例子能帮到你。