关于WPF中Popup中的一些用法的总结

简介: 原文:关于WPF中Popup中的一些用法的总结  Popup控件是一个常用的非常有用的控件,顾明思义就是弹出式控件,首先我们来看看MSDN对它的解释吧,表示具有内容的弹出窗口,这个是非常重要的控件,我们看看它的继承关系吧:  System.
原文: 关于WPF中Popup中的一些用法的总结

  Popup控件是一个常用的非常有用的控件,顾明思义就是弹出式控件,首先我们来看看MSDN对它的解释吧,表示具有内容的弹出窗口,这个是非常重要的控件,我们看看它的继承关系吧:

 System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.DependencyObject
      System.Windows.Media.Visual
        System.Windows.UIElement
          System.Windows.FrameworkElement
            System.Windows.Controls.Primitives.Popup

    Popup控件是从FrameworkElement直接继承而来的,属于非常高的层级,我们在使用中使用的最多的属性就是下面这些属性:1 PlacementTarget 表示Popup控件的放置的位置依赖的对象,这个通常使用绑定的方式来标明Popup控件停靠的目标。比如说:PlacementTarget="{Binding ElementName=PCheckBox}"  表示Popup停靠的位置依赖于一个名为PCheckBox的ChenkBox对象,这个也是经常使用的一种情况,我们可以将Popup控件和CheckBox,ToggleButton等一系列的控件来配合使用作出不同的效果。2 Placement属性:获取或设置的方向 Popup 控件时,控件将打开,并指定的行为 Popup 控制时与屏幕边界重叠。MSDN上面的解释是:您可以通过设置相关的属性来定位弹出的位置,通过设置 PlacementTargetPlacementRectanglePlacement、HorizontalOffset 和 VerticalOffsetProperty 属性来定位弹出项。3 其实这里PlacementRectangleHorizontalOffset 和 VerticalOffsetProperty这一对属性可以做一些等价的替换,这些都是可以对Popup的弹出的位置进行微调。4 IsOpen属性,这个是最重要的属性之一,通常是通过绑定的方式来为其进行赋值,比如说:IsOpen="{Binding ElementName=PCheckBox,Path=IsChecked}" 是通过绑定CheckBox的IsChecked属性来控制Popup的弹出。最后需要重点介绍的就是StayOpen属性,MSDN的解释是:获取或设置一个值,该值指示当 Popup 控件焦点不再对准时,是否关闭该控件。当将 StaysOpen 属性设为 true 时,Popup 始终处于打开状态,直到通过将 IsOpen 属性设置为 false 将其显式关闭。当 StaysOpen 设置为false 时,Popup 控件会截获所有鼠标事件和键盘事件,以确定在 Popup 控件之外发生这些事件之一,最明显的区别是当设置IsOpen 为True时弹出Popup控件,当使用鼠标在另外的地方进行点击时Popup失去焦点,同时Popup隐藏,而当StaysOpen 设置为True时,当Popup失去焦点时,Popup则不会隐藏,此时仍然会保持打开的状态。

       还有我们还可以设置一些Popup的弹出时的动画效果。我们可以设置PopupAnimation="Fade" 表示弹出时是通过渐入的方式进入的,这些在使用时需要注意。

       下面通过一个小例子来说明Popup的用法,通过TextBox和Popup配合使用来达到类似于百度搜索框的效果,首先贴出重点的实现代码:        

    <TextBox x:Name="dutyPersonTextBox"
         Text="{Binding DutyPersonName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
         Width="70" 
         Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}">
         <i:Interaction.Triggers>
             <i:EventTrigger EventName="TextChanged">
                 <interactive:ExInvokeCommandAction Command="{Binding DataContext.ModifyDutyPersonCommand,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=my:MainWindow}}"
                      CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}}">
                      </interactive:ExInvokeCommandAction>
              </i:EventTrigger>
              <i:EventTrigger EventName="GotFocus">
                  <interactive:ExInvokeCommandAction Command="{Binding DataContext.TextBoxGotFocus,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=my:MainWindow}}"
                    CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}}">
                    </interactive:ExInvokeCommandAction>
              </i:EventTrigger>
             </i:Interaction.Triggers>
        </TextBox>
        <Popup x:Name="popup"                         
               Width="{Binding ActualWidth,ElementName=dutyPersonTextBox}"
               IsOpen="{Binding  ElementName=dutyPersonTextBox,Path=IsKeyboardFocused,  Mode=OneWay}"
               StaysOpen="True">
               <Grid Background="Red">
                     <ListBox x:Name="lb_selecthistorymembers"                          
                              SnapsToDevicePixels="true" 
                              ItemsSource="{Binding DataContext.SpecificHistoryMembers,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=my:MainWindow},Mode=TwoWay}" 
                              HorizontalAlignment="Stretch" 
                              ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                              Background="#fff">
                              <i:Interaction.Triggers>
                                  <i:EventTrigger EventName="SelectionChanged">
                                      <interactive:ExInvokeCommandAction Command="{Binding DataContext.OnSelectHistoryMembersListBoxSelected,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=my:MainWindow},Mode=TwoWay}"
                                          CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}">
                                      </interactive:ExInvokeCommandAction>
                                  </i:EventTrigger>
                               </i:Interaction.Triggers>
                               <ListBox.ItemContainerStyle>
                                   <Style TargetType="ListBoxItem">
                                        <Setter Property="Template">
                                              <Setter.Value>
                                                   <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                                       <Border x:Name="Bd" 
                                                               Height="Auto" 
                                                               Width="Auto"
                                                               BorderBrush="{TemplateBinding BorderBrush}"
                                                               BorderThickness="{TemplateBinding BorderThickness}" 
                                                               Background="{TemplateBinding Background}" 
                                                               Padding="1" 
                                                               SnapsToDevicePixels="true">
                                                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                                       </Border>
                                                     <ControlTemplate.Triggers>
                                                           <Trigger Property="IsEnabled" 
                                                                    Value="false">
                                                               <Setter Property="Foreground" 
                                                                       Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                                           </Trigger>
                                                     </ControlTemplate.Triggers>
                                                  </ControlTemplate>
                                                </Setter.Value>
                                             </Setter>
                                             <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
                                             <Setter Property="VerticalAlignment" Value="Center"></Setter>
                                             <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                                          </Style>
                                      </ListBox.ItemContainerStyle>
                                      <ListBox.ItemsPanel>
                                          <ItemsPanelTemplate>
                                              <StackPanel Background="White"
                                                          IsItemsHost="True"
                                                          HorizontalAlignment="Left"
                                                          VerticalAlignment="Center"
                                                          Width="{Binding ActualWidth,ElementName=dutyPersonTextBox}">
                                                </StackPanel>
                                            </ItemsPanelTemplate>
                                            </ListBox.ItemsPanel>
                                            <ListBox.ItemTemplate>
                                                  <DataTemplate>
                                                       <Border  Name="Border" 
                                                                Padding="2" 
                                                                SnapsToDevicePixels="true"                                           
                                                                BorderThickness="1">
                                                                <Grid>
                                                                  <Label Content="{Binding SpecificHistoryDutyPersonName}"
                                                                         HorizontalAlignment="Stretch"
                                                                         HorizontalContentAlignment="Left"
                                                                         FontSize="13">
                                                                  </Label>
                                                                </Grid>
                                                       </Border>
                                                           <DataTemplate.Triggers>
                                                                <Trigger Property="IsMouseOver" 
                                                                         Value="true">
                                                                     <Setter Property="Background"
                                                                             Value="#00a3d9"
                                                                             TargetName="Border">
                                                                     </Setter>
                                                                     <Setter Property="Opacity"
                                                                             Value="0.6"
                                                                             TargetName="Border">
                                                                     </Setter>
                                                                        </Trigger>
                                                                    </DataTemplate.Triggers>
                                                                </DataTemplate>
                                                            </ListBox.ItemTemplate>
                                                        </ListBox>
                                                    </Grid>
                                                </Popup>

  最终实现的效果,如下所示:        

目录
相关文章
|
5月前
|
前端开发 C#
浅谈WPF之Popup弹出层
在日常开发中,当点击某控件时,经常看到一些弹出框,停靠在某些页面元素的附近,但这些又不是真正的窗口,而是页面的一部分,那这种功能是如何实现的呢?今天就以一个简单的小例子,简述如何在WPF开发中,通过Popup实现鼠标点击弹出浮动停靠窗口,仅供学习分享使用,如有不足之处,还请指正。
142 0
|
C#
【WPF】使用Popup控件做浮窗/提示框
原文:【WPF】使用Popup控件做浮窗/提示框 需求:当鼠标移入某个区域时,弹出一个浮窗,以便用户进行下一步操作。 效果如下图: 当鼠标移入左上角的【多选显示】框内,出现下面的浮窗(悬浮在原UI之上)。
4369 0
|
C#
WPF中StringFormat的用法--显示特定位数的数字
原文:WPF中StringFormat的用法--显示特定位数的数字 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangli321456/article/details/80324458 ...
1606 0
|
C#
WPF中StringFormat的用法
原文:WPF中StringFormat的用法 WPF中StringFormat的用法可以参照C#中string.Format的用法 1、 C#中用法: 格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元)示例: string.
4166 0
|
C#
关于wpf中popup跟随鼠标移动显示
原文:关于wpf中popup跟随鼠标移动显示 最近在做一个画图工具,里面有一个功能是需要实现,当鼠标移动的时候在,鼠标的旁边显示坐标信息。 第一反应是想到了tooltip,但是tooltip有许多的限制,查询资料的过程中看到了popup,popup比tooltip更加灵活,下面讲讲tooltip跟popup的区别: 1.tooltip会自动显示,自动隐藏,而popup则需要设置IsOpen属性,并且在Popup.StaysOen属性为true时,Popup控件会一直显示,直到显式地将IsOpen属性设置为False。
1989 0
|
C#
WPF中Popup的几个问题
原文:WPF中Popup的几个问题 要用popup控件来解决一些问题。就此带来了一批问题。 问题一、 在popup外任意位置点击时要能关闭popup,这个本来简单,只要加上StaysOpen=false就可以了。
1383 0
WPF listview item mouse enter/over popup
This is because the routing strategy of the Loaded event is Direct, which means that the routed event does not route though an element tree.
984 0
|
前端开发 C#
WPF备忘录(1)有笑脸,有Popup
原文:WPF备忘录(1)有笑脸,有Popup 1.画个笑脸给大家娱乐一下: 效果如下:   2.
948 0
|
C#
WPF中Popup等弹窗的位置不对(偏左或者偏右)
原文:WPF中Popup等弹窗的位置不对(偏左或者偏右) 1.情况如图:    正常情况:         部分特殊情况:        在一般的电脑都能正确显示,就是第一种情况,同样的代码为什么在不同的电脑就会显示不同的位置呢,原来Windows为了满足 不同需求的用户,左撇子和右撇子,就...
1848 0