WPF弹出窗口仅在给定区域

时间:2017-08-16 18:11:12

标签: c# wpf listbox popup

我需要一些建议。我有一个ListBox,它在Wrappanel中有已排序的项目。当我将鼠标指针悬停在ListBox中的某个项目上时,我希望看到类似Popup的内容,以便它只能在控件区域(ListBox)中看到并具有静态位置。

与图片相似的东西: enter image description here

我们只记录了这一点。我的弹出窗口也在ListBox区域之外。

见图片:enter image description here

我的想法:enter image description here

我目前的代码:

<DataTemplate x:Key="ItemTemplate">
    <Grid Effect="{DynamicResource z-depth2}" 
    Width="185" Height="278" 
    Background="{DynamicResource RG_WindowBackgroundBrush}" 
    SnapsToDevicePixels="True" 
    UseLayoutRounding="True">
        <Grid.Resources>
            <DropShadowEffect x:Key="z-depth2" BlurRadius="8" ShadowDepth="2.5" Direction="270" Color="{DynamicResource RG_DropShadowEffect}" />
        </Grid.Resources>
         <Rectangle x:Name="Rectangle1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="0" Height="0" />
        <controls:ItemTracer HorizontalAlignment="Stretch"
                            Container="{Binding ., RelativeSource={RelativeSource AncestorType={x:Type controls:VirtualizingTilePanel}}}" 
                            ContainerWidth="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type controls:VirtualizingTilePanel}}}"                                               
                            ItemIndex="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"  
                            ItemsCount="{Binding Items.Count, RelativeSource={RelativeSource AncestorType={x:Type controls:AnimatedListBox}}}"               
                            x:Name="lastInLine"/>
        <cachedImage:Image Width="185" Height="278" Stretch="UniformToFill"  ImageUrl="{Binding MoviePoster}" 
        RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.EdgeMode="Aliased" SnapsToDevicePixels="True"
        UseLayoutRounding="True" HorizontalAlignment="Left" VerticalAlignment="Top"/>

      <controls:NonTopmostPopup x:Name="Popup" IsOpen="False" Width="400" Height="200"   
                                PlacementTarget="{Binding ElementName=Rectangle1}"     
                                AllowsTransparency="True"  
                                PopupAnimation="None"  
                                VerticalOffset="0">
            <controls:NonTopmostPopup.Style>
                <Style  TargetType="{x:Type controls:NonTopmostPopup}">
                    <Setter Property="HorizontalOffset" Value="0" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="0">
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsLastItem, ElementName=lastInLine}" Value="true"  >
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsLastInLine, ElementName=lastInLine}" Value="true"  >
                            <Setter Property="HorizontalOffset" Value="-200" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" Value="1">
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </controls:NonTopmostPopup.Style>
            <Grid Background="Blue">

            </Grid>
       </controls:NonTopmostPopup>
    </Grid>
         <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorLevel=1, AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">                   
                <Setter TargetName="Popup" Property="IsOpen" Value="True" />
            </DataTrigger>
        </DataTemplate.Triggers>

 <Style x:Key="ListBoxMovieItem" TargetType="{x:Type ListBoxItem}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
    <Setter Property="Foreground" Value="{DynamicResource RG_ForegroundBrush}" />
    <Setter Property="Margin" Value="2"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}" >
                    <ContentPresenter Panel.ZIndex="1"/> 
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="IsSelected" Value="True"/>
                    </Trigger>      
                </ControlTemplate.Triggers>
            </ControlTemplate>              
        </Setter.Value>
    </Setter>
</Style>

 <Style TargetType="{x:Type controls:AnimatedListBox}">
    <Setter Property="ItemContainerStyle" Value="{DynamicResource ListBoxMovieItem}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                    <controls:VirtualizingTilePanel 
                    ChildHeight="300" x:Name="VirtualizingTilePanel"
                    ChildWidth="220"
                    IsVirtualizing="True"
                    VirtualizationMode="Recycling" />     
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type controls:AnimatedListBox}">
                    <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1">
                        <controls:AnimatedScrollViewer 
                            x:Name="PART_AnimatedScrollViewer"
                            CanKeyboardScroll="True"
                            Padding="{TemplateBinding Padding}" 
                            Focusable="false"
                            ScrollingSpline="0,0.50,0.50,1" ScrollingTime="00:00:00.8000000"    >
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </controls:AnimatedScrollViewer>
                    </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<controls:AnimatedListBox x:Name="AnimatedListBox"
              ScrollViewer.CanContentScroll="True"
              VirtualizingStackPanel.IsVirtualizing="True"
              VirtualizingStackPanel.VirtualizationMode="Recycling"                   
              ItemsSource="{Binding MoviesCvs.View, IsAsync=True}"/>                 

非常感谢。请原谅我的英文

0 个答案:

没有答案