原文:关于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上面的解释是:您可以通过设置相关的属性来定位弹出的位置,通过设置 PlacementTarget、PlacementRectangle、Placement、HorizontalOffset 和 VerticalOffsetProperty 属性来定位弹出项。3 其实这里PlacementRectangle和HorizontalOffset 和 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>

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

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

  1. c# js popup_关于WPF中Popup中的一些用法的总结

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

  2. 关于WPF的ComboBox中Items太多而导致加载过慢的问题

                                         [WFP疑难]关于WPF的ComboBox中Items太多而导致加载过慢的问题                         ...

  3. @ini_get php,php中get_cfg_var()和ini_get()的用法及区别_php技巧_脚本之家

    本文实例讲述了php中get_cfg_var()和ini_get()的用法及区别.分享给大家供大家参考.具体分析如下: php里get_cfg_var()和ini_get()都是取得配置值的函数,当你 ...

  4. java7 javascript引擎_Java7中脚本引擎的一般用法,共三种方法获得JavaScript引擎:名称、文件扩展名、MIME类型 | 学步园...

    package com.sino.java7; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; i ...

  5. Oracle中INSTR和SUBSTR的用法

    2019独角兽企业重金招聘Python工程师标准>>> Oracle中INSTR和SUBSTR的用法 Oracle中INSTR的用法: INSTR方法的格式为 INSTR(源字符串, ...

  6. python threading join_Python中threading模块join函数用法实例分析

    本文实例讲述了Python中threading模块join函数用法.分享给大家供大家参考.具体分析如下: join的作用是众所周知的,阻塞进程直到线程执行完毕.通用的做法是我们启动一批线程,最后joi ...

  7. sklearn中cross_val_score、cross_val_predict的用法比较

    sklearn中cross_val_score.cross_val_predict的用法比较_程大海的博客-CSDN博客_cross_val_predict

  8. MapInfo中常用查询函数及用法

    MapInfo中常用查询函数及用法: 函数用途 语法 备注 图层中选点 Str$(obj)="point": Str(String)表示字符串:point表示点: 图层中选线 St ...

  9. 【Java学习笔记之二十九】Java中的equals和==的用法及区别

    Java中的"equals"和"=="的用法及区别 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String(&quo ...

最新文章

  1. 小学六年级下册计算机计划,小学六年级科学下册教学计划(通用5篇)
  2. 用正则表达式验证php用户注册系统,php用户注册时常用的检验函数实例总结
  3. Ben Horowitz:执行程序有多糟糕,公司倒闭就有多快
  4. Android开发之Intent.Action
  5. Vue简单封装axios—解决post请求后端接收不到参数问题
  6. 7-2 个位数统计 (15 分)
  7. 8月13日见!三星Note 20国行版官宣:唯一悬念只剩价格
  8. nginx服务+LEMP搭建
  9. 自创解决键盘最后退出的问题
  10. 带你了解关系网络在反欺诈领域的常见应用
  11. 【Python刷题篇】——Python入门 01 输入输出
  12. php递归源码,PHP递归算法的实例程序
  13. apk编辑器找Android,教你用安卓神器APK编辑器去除程序广告
  14. 差异性分析该如何选择?
  15. Win10笔记本电脑硬盘如何分区
  16. win10/11 paddlepaddle2.3/2.2 之 匹配CUDA和Cudnn版本安装
  17. 关于emjoy表情在android5.x以上系统触发jni错误的修改(基于cocos2dx2.1.5修改)
  18. nlp-with-transformers实战-01_transformers简介
  19. iOS开发之第三方支付微信支付教程,史上最新最全第三方微信支付方式实现、微信集成教程,微信实现流程
  20. 谷歌卫星地图导出MBTile离线包及应用教程

热门文章

  1. dedecms代码研究三
  2. OA,ERP等源码一部分演示
  3. Delphi中的基础数据类型
  4. python etree创建xml_Python构建XML树结构的实例教程
  5. 织梦自适应php源码,DEDE织梦PHP源码响应式建筑设计类网站织梦模板(自适应手机端)...
  6. Linux64位steam,这下没得玩了! Steam无奈抛弃Linux用户
  7. 冒泡排序的双重循环理解
  8. 实现页面适配_微信公众号文章页面适配深色模式
  9. 面试官问你B树和B+树,就把这篇文章丢给他
  10. 劳动节快乐 | 5月1日 星期六 | 喜马拉雅赴美递交IPO招股书;拼多多年活跃买家7.884亿;抖音电商开启“抖音55潮购季”