Wpf Popup Placement

Is it likely that I can place a popup next to an item from a ListBox? I am using MVVM, the list is tied to items, and for some selected items I want to show a popup next to the item.

I have a list of items and I want to show a popup when I click on a specified list item, but a popup should appear next to the selected list item.

I tried something like this (it doesn't work):

    <Popup  IsOpen="{Binding Path=ShowPopup}" PlacementTarget="{Binding ElementName=List1, Path=SelectedItem}" Placement="Center">
        <TextBox Background="Red" Height="120" Text="Aaaaaa FUUUUUUUUUUUUU....."></TextBox>
    </Popup>

I do not want to use the code behind, only xaml

+5
source share
3 answers

This will place a popup to the right of the selected ListBoxItem

alt text

Example

<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>
+6
source

, , - , , ui.

PlacementTarget="{Binding ElementName=List1, Path=SelectedItem}"

SelectedItem / , , PlacementTarget.

PlacementTarget ItemContainer (Dr. WPF ), "".

, , , .

+2

Since you want to show a popup when you click an item, this will work for you:

<Popup  IsOpen="{Binding Path=ShowPopup}" Placement="Mouse">
     <TextBox Background="Red" Height="120" Text="Aaaaaa FUUUUUUUUUUUUU....."></TextBox>
 </Popup>
+1
source

All Articles