我有没有机会在ListBox的项目旁边放置弹出窗口?我使用MVVM,list绑定到元素,对于一些选择的元素,我想在项目旁边显示弹出窗口.
我有元素列表,我想在单击指定的列表元素时显示弹出窗口,但弹出窗口应显示在所选列表项旁边.
我试过这样的事情(它不起作用):
<Popup IsOpen="{Binding Path=ShowPopup}" PlacementTarget="{Binding ElementName=List1, Path=SelectedItem}" Placement="Center">
<TextBox Background="Red" Height="120" Text="Aaaaaa FUUUUUUUUUUUUU....."></TextBox>
</Popup>
Run Code Online (Sandbox Code Playgroud)
我不想使用后面的代码,只有xaml
这会将弹出窗口放在所选ListBoxItem的右侧

例
<Window.Resources>
<SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<ControlTemplate x:Key="PopupListBoxItemTemplate" TargetType="ListBoxItem">
<Border Name="Border" Padding="2" SnapsToDevicePixels="true">
<Grid>
<Popup Name="c_popup" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" >
<Border BorderBrush="Black" BorderThickness="1" CornerRadius="2.5">
<TextBlock Background="Wheat" Foreground="Black" Text="Aaaaaa FUUUUUUUUUUUUU....."/>
</Border>
</Popup>
<ContentPresenter />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
<Setter TargetName="c_popup" Property="IsOpen" Value="True"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<ListBox Name="listBox"
ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template" Value="{StaticResource PopupListBoxItemTemplate}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
由于您想在单击该项目时显示弹出窗口,因此这对您有用:
<Popup IsOpen="{Binding Path=ShowPopup}" Placement="Mouse">
<TextBox Background="Red" Height="120" Text="Aaaaaa FUUUUUUUUUUUUU....."></TextBox>
</Popup>
Run Code Online (Sandbox Code Playgroud)