我是否有机会在 ListBox 中的项目旁边放置弹出窗口? 我使用 MVVM,列表绑定(bind)到元素,对于一些选择的元素,我想在项目旁边显示弹出窗口。
我有元素列表,我想在单击指定列表元素时显示弹出窗口,但弹出窗口应显示在所选列表项旁边。
我试过这样的事情(它不起作用):
<Popup IsOpen="{Binding Path=ShowPopup}" PlacementTarget="{Binding ElementName=List1, Path=SelectedItem}" Placement="Center">
<TextBox Background="Red" Height="120" Text="Aaaaaa FUUUUUUUUUUUUU....."></TextBox>
</Popup>
我不想使用代码隐藏,只有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>
关于c# - Wpf 弹出窗口放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3963759/