簡體   English   中英

Wpf Popup放置

[英]Wpf Popup placement

我有沒有機會在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>

我不想使用后面的代碼,只有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>

由於您希望在單擊項目時顯示彈出窗口,這是否適合您:

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

您的示例不起作用的原因僅僅是因為您將放置目標綁定到非ui對象。

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

在這種情況下,SelectedItem可以是表示列表中項目的模型/視圖模型,因此不能正確使用PlacementTarget屬性。

你需要的是將PlacementTarget設置為ItemContainer( Dr. WPF解釋 ),如果沒有“某些”代碼的幫助,這是不可能的。

現在你已經知道了這個問題,有幾種方法可以讓你的代碼工作,所以我會把它留給你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM