【问题标题】:Wpf Popup placementWpf 弹出窗口放置
【发布时间】:2011-04-27 04:46:19
【问题描述】:

我有没有机会在 ListBox 中的项目旁边放置弹出窗口? 我使用 MVVM,列表绑定到元素,对于一些选择的元素,我想在项目旁边显示弹出窗口。

我有元素列表,我想在单击指定列表元素时显示弹出窗口,但弹出窗口应显示在所选列表项旁边。

我尝试过这样的事情(它不起作用):

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

我不想使用后面的代码,只有 xaml

【问题讨论】:

    标签: c# wpf binding


    【解决方案1】:

    由于您想在单击项目时显示弹出窗口,这对您有用吗:

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

    【讨论】:

      【解决方案2】:

      您的示例不起作用的原因仅仅是因为您将放置目标绑定到非 ui 对象。

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

      在这种情况下,SelectedItem 可能是一个模型/视图模型,代表列表中的一个项目,因此 PlacementTarget 属性的使用不正确。

      您需要将 PlacementTarget 设置为 ItemContainer (Dr. WPF explains),如果没有“一些”代码的帮助,这是不可能的。

      既然您知道问题所在,有几种方法可以使您的代码正常工作,所以我将由您来决定。

      【讨论】:

        【解决方案3】:

        这将在所选 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>
        

        【讨论】:

        • 很好的解决方案,但我发现了一个小错误。为了让它工作,我必须将 Setter 从 {StaticResource ListBoxItemTemplate} 更改为 StaticResource PopupListBoxItemTemplate}。
        猜你喜欢
        • 2012-07-15
        • 1970-01-01
        • 1970-01-01
        • 2017-09-06
        • 2011-09-04
        • 2016-05-10
        • 2017-07-28
        • 2017-08-13
        • 1970-01-01
        相关资源
        最近更新 更多