c# - Wpf 弹出窗口放置

标签 c# wpf binding

我是否有机会在 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 的右侧放置一个弹出窗口

alt text

例子

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

相关文章:

c# - session 变量不存储值

c# - 在 C# 中使用动态数据填充 Crystal Reports 等控件

C#检查字符是否为标记的方法

c# - 如何创建不同类型的数组

WPF GridSplitter 拆分/调整两个 ListBox 的大小?

c# - 我应该在哪里存储应用程序的 TrayIcon?

c# - Wpf - 单独程序集或文件夹中的 MVVM 和服务

java - 为什么 JAXB 说 "xxx is an interface, and JAXB can' t 处理接口(interface)”。即使生成的类不是接口(interface)

cocoa - 当项目更改时,CoreData 绑定(bind)的 NSTableView 会失去输入焦点,但前提是已排序

wpf - 绑定(bind)到多个索引器